The image below is a screenshot of 2 objects in an array.
How can I retrieve the dates and the string "" inside this array? Please consider if there is more than 1 object in the array.
I've created this function thus 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 you tweak this function to get what I want.
dump of object in chrome
Edit:
The object:
data = new google.visualization.DataTable();
data.addColumn('datetime', 'start');
data.addColumn('datetime', 'end');
data.addColumn('string', 'content');
A listener lets users to add events to the timeline, and I add the data to the timeline as follows:
var options = {
"width": "100%",
"height": "300px",
"editable": true,
"style": "box"
};
timeline.draw(data, options);
Here is the delete function (may help to distinguish selections of the object):
function doDelete() { /*** Delete the currently selected event */
// retrieve the 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 an event, then 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
}
-
2Can you supply the actual object instead of the image of the object?Jay Blanchard– Jay Blanchard2014年04月24日 14:13:29 +00:00Commented Apr 24, 2014 at 14:13
-
1Hard to understand structure of your js object in the screenshot, Would be nice if you could post your json.milagvoniduak– milagvoniduak2014年04月24日 14:14:18 +00:00Commented Apr 24, 2014 at 14:14
-
Hi, this is the API, just cant seem to find a GET ALL ITEMS() method. almende.github.io/chap-links-library/js/timeline/doc/jsdoc/…cwiggo– cwiggo2014年04月24日 14:19:43 +00:00Commented Apr 24, 2014 at 14:19
-
@Chris: Your link doesn't really help. Can you include in your question the actual object you have and the object you want to generate from it. This ought to be pretty easy to do.Matt Burland– Matt Burland2014年04月24日 14:24:47 +00:00Commented Apr 24, 2014 at 14:24
-
@MattBurland see edit, please let me know if that is enough insight for you.cwiggo– cwiggo2014年04月24日 14:29:50 +00:00Commented Apr 24, 2014 at 14:29
3 Answers 3
You seem to have 4 levels actually, and have only shown the .rows
array of your data
object in the screenshot. However, you should not just wrap another loop around it, but explicitly access that property - just as you have done with .v
in the end. Also, the .c
property should similarily be accessed explicitly instead of just being enumerated.
function printData(data) {
console.log(JSON.stringify(data)); // assuming this is the shown output
$.each(data.rows, function(i, row) {
$.each(row.c, function(j, item) {
console.log(item.v);
});
});
}
1 Comment
Maybe this works for you:
$(arr).each(function (index) {
$(arr[index]).each(function (index1) {
$(arr[index][index1]).each(function (index2) {
alert(arr[index][index1][index2]);
});
});
});
Comments
Try this instead:
//obj - your object
$.each(obj.rows, function(i,rows){
$.each(rows, function(j, c){
$.each(c, function(k, data){
console.log("f: " + data.f + "v: " + data.v);
});
});
});
2 Comments
rows
but the full object you provided starts from cols
. In which format you're passing data
to your printData()
?