Tuesday, January 1, 2019

javascript - How to iterate through a json object?






Possible Duplicate:
I have a nested data structure / JSON, how can access a specific value?






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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...