To get to the point, let's look at two examples first.
var arr=[' 1 ', ' 4 ', ' 9 ', ' 16 '];
var r=arr.map (MATH.SQRT);
Guess what the result of R would be?
That's right.
[1,2,3,4]
Let's try another one.
var arr=[' 1 ', ' 4 ', ' 9 ', ' 16 '];
var r=arr.map (parseint);
And guess what the result is?
is [1,2,3,4]?
Console try to see what the result is.
[1,nan,nan,1]!
Not surprised.
Actually, the truth ————
That's the number of arguments!
Let's look at one more example
var arr=[' 1 ', ' 4 ', ' 9 ', ' 16 '];
var r=arr.map (function (x) {return parseint (x)});
r//[1,2,3,4]
The result is not normal!
Actually in the call
Arr.map (parseint);
There are three parameters passed to map
Current value (Currentvaluve)//' 1 ', ' 4 ', ' 9 ', ' 16 '
Index of current value (CURRENTINDEX)//0,1,2,3
and the current array (currentarray)//[' 1 ', ' 4 ', ' 9 ', ' 16 '], each time this
Each time the parseint () function is used, only two values (Currentvalue,currentindex) are passed in.
So the result is:
parseint (' 1 ', 0)//1
The second parameter, if passed after the number function is converted to 0 or NaN
, is ignored. --mozilla Official documents
parseint (' 4 ', 1)//illegal, NaN
parseint (' 9 ', 2)//illegal, NaN
parseint (' 16 ', 3)//here refers to the note, when parsing the string ' 16 ', found that 6 is greater than or equal to 3, so the subsequent numbers are ignored, leaving only one 1 returned
If parseInt
a character is encountered that is not part of the radix
cardinality specified by the parameter, then both the character and the character after it are ignored. --mozilla Official documents
When we finally know the truth, we must pay attention to the problem of the number of callback parameters in the map function!
var r=arr.map (function (x) {return parseint (x)});
Questions about callback function parameters in the map method of array