How to it manipulate JSON multi-dimensional arrays using jQuery

How would you iterate over this JSON string after converting it to an object using jQuery to access the Color property?

[{"Puff":[{"Color":"Gray"},{"Color":"Blue"}]},{"Puff":[{"Color":"DarkRed"}]},{"Puff":[{"Color":"DarkBlue"},{"Color":"Yellow"}]}]

Answers


You have an array of objects, which all have a key named "Puff" which contains another array of objects.

$.each(x, function(i) {
    console.log(i);
    $.each(this.Puff, function() {
       console.log(this.Color);
    });
});

0
Gray
Blue
1
DarkRed
2
DarkBlue
Yellow

I would opt for non jquery: http://jsfiddle.net/xguyj/

var x = [{"Puff":[{"Color":"Gray"},{"Color":"Blue"}]},{"Puff":[{"Color":"DarkRed"}]},{"Puff":[{"Color":"DarkBlue"},{"Color":"Yellow"}]}];

for(var i = 0; i < x.length; i++){
    for(var t = 0; t < x[i].Puff.length; t++) {
         console.log(x[i].Puff[t].Color);
    }
}

Need Your Help

Check Google site if page name already exists when uploading folders

google-apps-script

I am uploading folders to a created google site using appscript, but sometimes an error occures during which the code is uploading the filenames to the site as a parentpage and does not finish uplo...

how to limit search of files to only specific extensions

python python-2.7 file-extension

I have one basic question (#1 below) and a question for which I have no idea (#2) about the answer. Can anyone provide inputs?