You can use a for
loop or the Array forEach()
method to loop through JSON Array in JavaScript. In this tutorial, you will learn how to loop the array of JSON objects in JavaScript.
JavaScript loop through JSON Array examples
Simple example code Using a for
loop. Use a for
loop to iterate over each object in the array, and log the name
and age
properties to the console for each object.
javascriptCopy codeconst jsonArray = [
{ name: 'John', age: 28 },
{ name: 'Jane', age: 32 },
{ name: 'Bob', age: 45 }
];
for (let i = 0; i < jsonArray.length; i++) {
console.log(jsonArray[i].name, jsonArray[i].age);
}
Using the Array.forEach()
method:
The forEach()
method to iterate over each object in the array, and log the name
and age
properties to the console for each object. Note that we use an arrow function to define the callback function for the forEach()
method.
<!DOCTYPE html>
<html>
<body>
<script >
const jsonArray = [
{ name: 'John', age: 28 },
{ name: 'Jane', age: 32 },
{ name: 'Bob', age: 45 }
];
jsonArray.forEach((item) => {
console.log(item.name, item.age);
});
</script>
</body>
</html>
Output:
Do comment if you have any doubts or suggestions on this JS JSON Array topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
Hi Rohit,
Have you tried using the console.table(jsonArray)? Also, it is not really a JSON array, just a JS array.
Regards, Tracy