preface
[1,2,3], but the real answer is [1, NaN, NaN]. Why? Let’s first look at the array.map () function’s definition in MDN
Array.map()
The map() method returns a new array, the result of which is returned when each element in the array calls one of the provided functions
var arr = [1.2.3];
var arr1 = arr.map(x= > x * 1);
console.log(arr1); / / 1, 2, 3
Copy the code
You can see that the map method accepts a function that iterates over each element in the array
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
// Return element for new_array
}[, thisArg])
Copy the code
The callback takes three arguments in total: currentValue is the current element being processed in the array, index is the index of the current element being processed in the array, and array is the array from which the map method was called
ThisArg optional, the value of this used when executing the callback function
parseInt(string, radix)
Radix resolves the cardinality of strings, and the cardinality rules are as follows:
- The interval ranges from 2 to 36
parseInt('123'.5) // Treat '123' as a base 5 number and return the decimal number 38 => 1*5^2 + 2*5^1 + 3*5^0 = 38 Copy the code
- When the argument is 0, parseInt() is parsed in decimal
- If ignored, the default cardinality rule is:
- If a string begins with “0x”, parseInt() parses the rest of the string into a hexadecimal integer
parseInt("0xf") / / 15 Copy the code
- If a string begins with a 0, the following characters are resolved to octal or hexadecimal numbers
parseInt("08") // 0 * 8 ^ 1 + 8 * 8 ^ 0 = 8 Copy the code
- If a string begins with a number from 1 to 9, parseInt() will parse it as a decimal integer
parseInt("88.99 f") / / 88 Copy the code
- Only the first digit in the string is returned
parseInt("10.33") / / 10 Copy the code
- Opening and closing Spaces are allowed
parseInt("69 10") / / 69 Copy the code
- If the first character of the string cannot be converted to a number, NaN is returned
parseInt("f") // NaN parseInt("f".16) / / 15 Copy the code
- If a string begins with “0x”, parseInt() parses the rest of the string into a hexadecimal integer
[‘1’, ‘2’, ‘3’].map(parseInt)
[‘1’, ‘2’, ‘3’]
['1'.'2'.'3'].map(function(cur,index){
return parseInt(cur, index)
})
Copy the code
So what the code does is
parseInt('1'.0); // Radix 0, use the default base 10 -> 1
parseInt('2'.1); // The radix value range is 2-36, unable to resolve, beyond the range -> NaN
parseInt('3'.2); | | | | | | | | | | | | | | | | | | | | | | | |
Copy the code
So the end result is: [1, NaN, NaN]