0

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
}
Brian Tompsett - 汤莱恩
5,92772 gold badges63 silver badges135 bronze badges
asked Apr 24, 2014 at 14:10
10
  • 2
    Can you supply the actual object instead of the image of the object? Commented Apr 24, 2014 at 14:13
  • 1
    Hard to understand structure of your js object in the screenshot, Would be nice if you could post your json. Commented 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/… Commented 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. Commented Apr 24, 2014 at 14:24
  • @MattBurland see edit, please let me know if that is enough insight for you. Commented Apr 24, 2014 at 14:29

3 Answers 3

1

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);
 });
 });
}
answered Apr 24, 2014 at 15:20
Sign up to request clarification or add additional context in comments.

1 Comment

I didnt know the data was in a raw format, you have to parse the data object with JSON, thanks for taking the time to finding a solution! Thanks a billion. Reputation++;
0

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]);
 });
 });
});

DEMO

answered Apr 24, 2014 at 14:18

Comments

0

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);
 });
});
});

JsFiddle

answered Apr 24, 2014 at 14:51

2 Comments

That seems to work in the JSFiddle, but I can't get it to work within my code. I thought the variable data would be the object in question. I'm getting a "Cannot read property 'length' of undefined" error when running what you've put on the data object
@Chris Looks like the object shown in screenshot starts from only rows but the full object you provided starts from cols. In which format you're passing data to your printData() ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.