一、定义和用法
forEach会循环遍历数组的每一项,与map的功能类似但是map更注重的是返回值不会改变原数组,而forEach没有返回值并且会改变原数组!
二、参数 array.forEach((item,index,arr))
forEach接受一个回调函数,回调函数接受三个参数,item是当前遍历项、index是当前遍历项的索引值、arr为当前遍历的数组。通常只会用到第一个参数。
三、返回值
无
四、用例
const arr = [1,2,3]
//使arr的每一项都+1
arr.forEach((item,index,arr) => {
item++
}
)
console.log(arr) // [2,3,4]