forEach()方法用于调用数组的每个元素,并将元素传递给回调函数
主要使用场景:遍历数组的每个元素
语法
遍历的数组.forEach(function(当前数组元素,当前元素索引号){//函数体})
const arr=['red','green','blue'] arr.forEach(function(item,index){ console.log(item,index) })
注意:
forEach 主要是遍历数组
参数当前数组元素是必须要写的,索引号可选
forEach()方法用于调用数组的每个元素,并将元素传递给回调函数
主要使用场景:遍历数组的每个元素
语法
遍历的数组.forEach(function(当前数组元素,当前元素索引号){//函数体})
const arr=['red','green','blue'] arr.forEach(function(item,index){ console.log(item,index) })
注意:
forEach 主要是遍历数组
参数当前数组元素是必须要写的,索引号可选