0
点赞
收藏
分享

微信扫一扫

map()函数的使用

木樨点点 2022-03-19 阅读 70

1.概念

map()方法定义在JavaScript的Array中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值

注意:

  • map()不会对空数组进行检测
  • map()不会改变原始数组

2.语法

array.map(function(currentValue, index, arr), thisIndex)

参数说明:

  • function(currentValue, index, arr)必须。为一个函数,数组中的每个元素都会执行这个函数。其中函数参数:

  • thisValue可选。对象作为该执行回调时使用,传递给函数,用作"this"的值。


3.实例

返回由原数组中每个元素的平方组成的新数组:

let array = [1, 2, 3, 4, 5];

let newArray = array.map((item) => {
    return item * item;
})

console.log(newArray)  // [1, 4, 9, 16, 25]
举报

相关推荐

0 条评论