溫馨提示×

json數(shù)組循環(huán)取值的方法是什么

小億
608
2024-02-23 12:19:27
欄目: 編程語言

可以通過以下方法循環(huán)遍歷JSON數(shù)組并取值:

  1. 使用JavaScript的for循環(huán):
const jsonArray = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}];
for (let i = 0; i < jsonArray.length; i++) {
    console.log(jsonArray[i].name);
    console.log(jsonArray[i].age);
}
  1. 使用JavaScript的forEach方法:
const jsonArray = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}];
jsonArray.forEach(item => {
    console.log(item.name);
    console.log(item.age);
});
  1. 使用ES6的for…of循環(huán):
const jsonArray = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}];
for (const item of jsonArray) {
    console.log(item.name);
    console.log(item.age);
}

這些方法可以用來循環(huán)遍歷JSON數(shù)組并取出需要的值。

0