Tuesday, 28 February 2017

javascript - How to iterate through a json object?











I want to iterate through a json object which is two dimensional ...
for a one dimensional json object I do this



for (key in data) {
alert(data[key]);

}


what do i do about a two dimensional one??


Answer



There is no two dimensional data in Javascript, so what you have is nested objects, or a jagged array (array of arrays), or a combination (object with array properties, or array of objects). Just loop through the sub-items:



for (var key in data) {
var item = data[key];
for (var key2 in item) {

alert(item[key2]);
}
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...