jquery - Retrieving data from javascript object -
the image below screenshot of 2 objects in array.
how can retrieve dates , string "" inside array? please consider if there more 1 object in array.
i've created function far:
function printdata(data) { $.each(data, function(i, item) { //console.log(item); $.each(item, function(j, c) { //console.log(c); $.each(c, function(k, l){ //console.log(l.v); }); }); }); //console.log(data.c.v); console.dir(data); }
can tweak function want.
edit:
the object:
data = new google.visualization.datatable(); data.addcolumn('datetime', 'start'); data.addcolumn('datetime', 'end'); data.addcolumn('string', 'content');
a listener lets users add events timeline, , add data timeline follows:
var options = { "width": "100%", "height": "300px", "editable": true, "style": "box" }; timeline.draw(data, options);
here delete function (may distinguish selections of object):
function dodelete() { /*** delete selected event */ // retrieve selected row var sel = timeline.getselection(); if (sel.length) { if (sel[0].row != undefined) { var row = sel[0].row; } } if (row != undefined) { timeline.deleteitem(row); } else { alert("first select event, press remove again"); } } console.log(json.stringify(data, null, 4));
outputs:
{ "cols": [ { "id": "", "label": "start", "pattern": "", "type": "datetime" }, { "id": "", "label": "end", "pattern": "", "type": "datetime" }, { "id": "", "label": "content", "pattern": "", "type": "string" } ], "rows": [ { "c": [ { "v": "date(2014, 3, 24)", "f": null }, { "v": "date(2014, 4, 1)", "f": null }, { "v": "subgoal a", "f": null } ] }, { "c": [ { "v": "date(2014, 4, 1)", "f": null }, { "v": "date(2014, 4, 8)", "f": null }, { "v": "subgoal b", "f": null } ] } ], "p": null }
you seem have 4 levels actually, , have shown .rows
array of data
object in screenshot. however, should not wrap loop around it, explicitly access property - have done .v
in end. also, .c
property should similarily accessed explicitly instead of being enumerated.
function printdata(data) { console.log(json.stringify(data)); // assuming shown output $.each(data.rows, function(i, row) { $.each(row.c, function(j, item) { console.log(item.v); }); }); }
Comments
Post a Comment