温馨提示×

json数组循环取值的方法是什么

小亿
230
2024-02-23 12:19:27
栏目: 编程语言

可以通过以下方法循环遍历JSON数组并取值:

  1. 使用JavaScript的for循环:
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循环:
const jsonArray = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}];
for (const item of jsonArray) {
    console.log(item.name);
    console.log(item.age);
}

这些方法可以用来循环遍历JSON数组并取出需要的值。

0