JavaScript中的遍历总结 forEach(..)、map()for..of、 for..in、

13 阅读1分钟

forEach()

对数组的每个元素执行一遍给定的函数。

// 箭头函数
forEach((element) => { /* … */ })
forEach((element, index) => { /* … */ })
forEach((element, index, array) => { /* … */ })

// 回调函数
forEach(callbackFn)
forEach(callbackFn, thisArg)

// 内联回调函数
forEach(function(element) { /* … */ })
forEach(function(element, index) { /* … */ })
forEach(function(element, index, array){ /* … */ })
forEach(function(element, index, array) { /* … */ }, thisArg)

描述:forEach按升序为数组中含有效值的每一项执行一次callbackFn函数,那些已经删除或者未初始化的项将被跳过。

map()