Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do I iterate over a JSON structure?

I have the following JSON structure:
[{ "id":"10", "class": "child-of-9" }, { "id": "11", "classd": "child-of-10" }]

How do I iterate over it using JavaScript?
by

2 Answers

sandhya6gczb
To iterate over JSON structure use

var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];

for (var i = 0; i < arr.length; i++){
document.write("<br><br>array index: " + i);
var obj = arr[i];
for (var key in obj){
var value = obj[key];
document.write("<br> - " + key + ": " + value);
}
}
Sonali7
To iterate over JSON structure we can use for...of:
var mycars = [{name:'Susita'}, {name:'BMW'}];

for (var car of mycars)
{
document.write(car.name + "<br />");
}

Login / Signup to Answer the Question.