Home > Web Front-end > JS Tutorial > How Do I Iterate Through a JSON Array of Objects in JavaScript?

How Do I Iterate Through a JSON Array of Objects in JavaScript?

Susan Sarandon
Release: 2024-11-28 02:33:09
Original
954 people have browsed it

How Do I Iterate Through a JSON Array of Objects in JavaScript?

Iterating Over JSON Structures in JavaScript

When working with JSON data, it often becomes necessary to iterate over its structure to extract or manipulate data. In the provided JSON structure:

[
  {
    "id": "10",
    "class": "child-of-9"
  },
  {
    "id": "11",
    "class": "child-of-10"
  }
]
Copy after login

We can use a simple JavaScript for loop to iterate through each object in the array:

for (var i = 0; i < arr.length; i++) {
  // First, access the current object
  var obj = arr[i];

  // Now, we can iterate over the object's properties
  for (var key in obj) {
    // Obtain the value for the current property
    var value = obj[key];

    // Output the key and value
    document.write(`<br><br> - ${key}: ${value}`);
  }
}
Copy after login

This code will step through each object in the array, accessing its id and class properties. The output will be displayed in the browser, listing each property and its value.

The above is the detailed content of How Do I Iterate Through a JSON Array of Objects in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template