Why does parseInt yield NaN with Array#map?

前端 未结 7 1464
故里飘歌
故里飘歌 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:44

    You can use arrow function ES2015/ES6 and just pass number to the parseInt. Default value for radix will be 10

    [10, 20, 30].map(x => parseInt(x))
    

    Or you can explicitly specify radix for better readability of your code.

    [10, 20, 30].map(x => parseInt(x, 10))
    

    In example above radix explicitly set to 10

提交回复
热议问题