Why does parseInt yield NaN with Array#map?

前端 未结 7 1474
故里飘歌
故里飘歌 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 12:01

    another (working) quick fix :

    var parseInt10 = function(x){return parseInt(x, 10);}
    
    ['0', '1', '2', '10', '15', '57'].map(parseInt10);
    //[0, 1, 2, 10, 15, 57]
    

提交回复
热议问题