foreach循环json数组

58 阅读1分钟

定义一组数据如下

var obj = [
  {
    name: "a",
    age: 1,
  },
  {
    name: "b",
    age: 2,
  },
  {
    name: "c",
    age: 3,
  },
];

传入一个参数

obj.forEach(function (element) {
  console.log(element);
});

一个参数的结果 可见:一个参数时,foreach 循环的是 json 数组中的每个对象

传入两个参数

obj.forEach(function (element, index) {
  console.log(element);
  console.log(index);
});

两个参数结果 可见:两个参数时,第一个参数是 json 数组中的对象,第二个参数是对应的 index 值