Why does parseInt yield NaN with Array#map?

前端 未结 7 1593
故里飘歌
故里飘歌 2020-11-21 11:29

From the Mozilla Developer Network:

[1,4,9].map(Math.sqrt)

will yield:

[1,2,3]

Why then does this:

<
7条回答
  •  迷失自我
    2020-11-21 11:53

    map is passing along a 2nd argument, which is (in many of the cases) messing up parseInt's radix parameter.

    If you're using underscore you can do:

    ['10','1','100'].map(_.partial(parseInt, _, 10))

    Or without underscore:

    ['10','1','100'].map(function(x) { return parseInt(x, 10); });

提交回复
热议问题