Shaoli's Blog

js Array map() 详解
array.map(function(currentValue,index,arr), thisValue)


  • currentValue 当前元素的值
  • index 当前元素的索引值
  • arr 当前元素所属的数组
  • thisValue 可选参数。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue,或者传入 null、undefined,那么回调函数的 this 为全局对象。




以下会得到什么结果呢?

["1", "2", "3"].map(parseInt)




答案是

[1, NaN, NaN]


这是为什么呢?

因为map接收一个回调函数,默认有3个参数。

currentValue,
index,
arr


而parseInt只有2个参数,所以第三个arr参数自动忽略。

相当于

parseInt("1", 0) //1
parseInt("2", 1) //NaN
parseInt("3", 2) //NaN

    评论列表

  • 暂无评论...快来说说吧!
person
0 / 16
comment
0 / 100