1, the console. The log ([‘ 1 ‘, ‘2’, ‘3’]. Map parseInt ());

A JavaScript Optional Argument Hazard

  • Answer: [1, NaN NaN]

  • 解析 : this question, answer Lord I know the answer, because this question is a net red question to parse him let’s have a look first

Map the parameters of the method or callback function, and its use

/ / this is MDN
var array1 = [1.4.9.16];
// pass a function to map
const map1 = array1.map(x= > x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]
Copy the code
  • grammar
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
 // Return element for new_array }[, 
thisArg])
Copy the code
  • parameter

  • callback

A function that generates a new array element, taking three arguments:

  • currentValue

    The current element being processed in the callback array.

  • ** Index is optional ** Look at this and notice this parameter, think about it

    The index of the current element being processed in the callback array.

  • An array of optional

    The array in which the callback map method was called.

ThisArg optional

The value of this used when the callback function is executed.

  • Map is the prototype method of arrayparseInt, this method

What kind of spark will they have

why

  • Notice these two guys up here, and it’s easy to explain
  • Why: It ismapthecallbackThe second parameter index, the index of the current element, is taken asparseIntThe second parameter ofradixTo use

If you think about it, we thought our three calls looked like this

parseInt('1')
parseInt('2')
parseInt('3')
Copy the code

It’s actually called like this

parseInt('1'.0,theArray);
parseInt('2'.1,theArray);
parseInt('3'.2,theArray);
Copy the code

Ok, so how does index affect Radix?

  • So the first time, when I first called it, it was like this: parseInt(‘1’,0), that’s fine, it’s decalized look at my red box

    • Returns 1
  • The second time, the second index parameter is called with 1, which is also used as the basis for the value. If the base is non-0 or less than 2, the function will not query the string and will return NaN directly.

  • The third time, 2 is the base. This means that the string will be parsed into bytes, that is, containing only the values 0 and 1. Step 11 of the parseInt specification states that it only attempts to analyze the left side of the first character, which is not yet a significant digit requiring cardinality. The first character of this string is “3”, which is not a significant number in the base base 2. So the substring will be parsed to null. Step 12 says: If the substring parses to empty, the function returns NaN.

    • So the result here should be [1,NaN,NaN].

To solve

The problem here is that it’s easy to overlook that parseInt takes two arguments. Map has three parameters. So this is the combination that leads to the above problem. Write it another way:

['1'.'2'.'3'].map(function(value){
        return parseInt(value)
})
Copy the code

So you can’t go wrong.

Of course, we could also write:

['1','2','3'].map(Number)


I’m done here, but I’ll keep you updated