forEach
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。
注意: forEach() 对于空数组是不会执行回调函数的。
语法:
array.forEach(callback(currentValue, index, arr), thisValue)
- callback(currentValue, index, arr) 必需。 数组中每个元素需要调用的函数。
- currentValue 必需。当前元素
- index 可选。当前元素的索引值。
- arr 可选。当前元素所属的数组对象。
- thisValue 可选。传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值
- 返回值: undefined
map
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。map() 方法按照原始数组元素顺序依次处理元素。
注意:map() 不会对空数组进行检测。map() 不会改变原始数组。
语法:
array.map(callback(currentValue,index,arr), thisValue)
- callback(currentValue, index, arr) 必需。 函数,数组中的每个元素都会执行这个函数
- currentValue 必需。当前元素
- index 可