一、概念
map()方法定义在JavaScript的Array中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。
注意:
二、语法
array.map(function(currentValue, index, arr), thisIndex)参数说明:
function(currentValue, index, arr):必须。为一个函数,数组中的每个元素都会执行这个函数。其中函数参数: currentValue:必须。当前元素的的值。index:可选。当前元素的索引。arr:可选。当前元素属于的数组对象。 thisValue:可选。对象作为该执行回调时使用,传递给函数,用作"this"的值。三、实例
返回由原数组中每个元素的平方组成的新数组:
let array = [1, 2, 3, 4, 5];let newArray = array.map((item) => { return item * item;})console.log(newArray) // [1, 4, 9, 16, 25]