Tuesday, August 27, 2019

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

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...