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

来源:互联网 发布:国内erp软件排行 编辑:程序博客网 时间:2024/06/02 01:05

You may consider it [1,2,3].It is wrong. Let us figure it out.

map():

[1,2,3].map(function(a,b,c,d){return a+"-"+b+"-"+c+"-"+d;})//["1-0-1,2,3-undefined", "2-1-1,2,3-undefined", "3-2-1,2,3-undefined"]//first parameter:element which will be use//second parameter:index of the element//third parameter:array itself

parseInt(string,radix):

parseInt("1")    //1,the function will use 10 by default without radixparseInt("12a34")    //12,only the first number will be returnedparseInt("1",0)    //1,if the radix is 0,it will be treated as 10parseInt("1",1)    //NaN,if the radix is not 0 and is not between 2 and 36return NaN

So what we need to do is:
parseInt(“1”,0) //1
parseInt(“2”,1) //NaN
parseInt(“3”,2) //NaN

Then the result is [1,NaN,NaN]

0 0
原创粉丝点击