📜  loop json - Javascript (1)

📅  最后修改于: 2023-12-03 15:32:45.333000             🧑  作者: Mango

Looping JSON in Javascript

JSON (JavaScript Object Notation) is a lightweight data format widely used in web applications for data exchange. JSON data can be easily parsed in Javascript, and looping through JSON data is a common use case.

In this guide, we'll explore different ways to loop through JSON data in Javascript.

Prerequisites

You should have a basic understanding of Javascript and JSON syntax before proceeding with this guide.

Using for...in loop

The for...in loop is a traditional loop in Javascript used to iterate over objects. We can use it to loop through JSON data as well.

const json = {
  "name": "John Doe",
  "age": 30,
  "gender": "male"
};

for (let key in json) {
  console.log(key + ": " + json[key]);
}

In the above example, we define a JSON object json with three key-value pairs. We use the for...in loop to iterate over the object and log each key-value pair to the console.

Output:

name: John Doe age: 30 gender: male


```markdown
Explanation:

The for...in loop iterates over each key in the JSON object json. We use the key to access the corresponding value using json[key] and log it to the console.

Using Object.entries() method

The Object.entries() method returns an array containing all the key-value pairs of an object. We can use this method with a forEach() loop to loop through JSON data.

const json = {
  "name": "John Doe",
  "age": 30,
  "gender": "male"
};

Object.entries(json).forEach(([key, value]) => {
  console.log(key + ": " + value);
});

In the above example, we use the Object.entries() method to convert the JSON object json into an array of key-value pairs. We then use the forEach() loop to iterate over the array and log each key-value pair to the console.

Output:

name: John Doe age: 30 gender: male


```markdown
Explanation:

The Object.entries() method returns an array containing [key, value] pairs for each key-value pair in the JSON object json. We use destructuring to access the key and value variables from each pair and log them to the console.

Using Array.map() method

The Array.map() method returns a new array containing the results of calling a provided function on every element in the original array. We can use this method with a forEach() loop to loop through JSON data.

const json = {
  "name": "John Doe",
  "age": 30,
  "gender": "male"
};

const pairs = Object.entries(json).map(([key, value]) => {
  return key + ": " + value;
});

console.log(pairs);

In the above example, we use the Object.entries() method to convert the JSON object json into an array of key-value pairs. We then use the Array.map() method to create a new array with each element in the form of key: value. Finally, we log the new array to the console.

Output:

["name: John Doe", "age: 30", "gender: male"]


```markdown
Explanation:

The Array.map() method creates a new array with each element in the form of key: value. We use return statement to return the new element for each pair in the array. We then log the new array to the console.

Conclusion

In this guide, we explored different ways to loop through JSON data in Javascript. The for...in loop is a traditional loop used to iterate over objects. The Object.entries() method and Array.map() method provide more functional ways to iterate over JSON data. Choose the one that suits your needs and coding style.