Tuesday 27 August 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...