Home >Web Front-end >JS Tutorial >How to Iterate Through a JSON Array in JavaScript?
Iterating Through a JSON Structure in JavaScript
This article offers a practical solution to iterating over a JSON structure in JavaScript, addressing the specific question:
Question:
How do I iterate over the following JSON structure in JavaScript?
[{ "id": "10", "class": "child-of-9" }, { "id": "11", "classd": "child-of-10" }]
Answer:
The provided JSON structure is in an array format. To iterate over it, follow these steps:
var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"} ];
for (var i = 0; i < arr.length; i++) {
var obj = arr[i];
for (var key in obj) {
var value = obj[key]; document.write("<br><br>array index: " + i); document.write("<br> - " + key + ": " + value);
This code snippet will iterate over each object in the JSON array and display its properties and values in the document.
The above is the detailed content of How to Iterate Through a JSON Array in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!