['1','2','3'].map(parseInt) Detailed Explanation

A front-end interview question ['1', '2', '3'].map(parseInt), the result [1, NaN, NaN], many people do not understand, let me explain the following

First, let's take a look at the map function

The map method (Array) (JavaScript)
invokes the defined callback function on each element of the array and returns an array containing the results.
array1.map(function(a,b,c){ a is each item in the array i.e. '1''2''3' b is the subscript of the array i.e. 0 1 2 c is the original array i.e. array1 }) as follows example:




var arr = ['1','2','3'];
  function fn(num,a,c){
    
    
    console.log(num,a,c);  
    //num       a           c
    //1         0      ["1", "2", "3"]
    //2         1      ["1", "2", "3"]
    //3         2      ["1", "2", "3"]
    return num;
  }
  // console.log(arr.map(fn));

It can be seen from the above that if the map contains a function, its parameters will be passed to the function

Default parameter: When no input parameters are specified for map, all parameters will be used.

['1','2','3'].map(function(){
    
    
    console.log(arguments);   //arguments中包括全部三个参数
})

insert image description here

parseInt(string, radix)

Parameter description
string Required. The string to be parsed.
radix is ​​optional. Indicates the base of the number to parse. The value is between 2 ~ 36.
If this parameter is omitted or has a value of 0, the number will be parsed in base 10. If it starts with "0x" or "0X", it will be base 16.
If the argument is less than 2 or greater than 36, parseInt() will return NaN. (from MDN)

console.log(['1','2','3'].map(parseInt));  //[1, NaN, NaN]
// 等同于
['1','2','3'].map((item,index)=>{
    
    
    return parseInt(item,index)
})

parseInt('1', 0) -> 1 radix is ​​0, parseInt() will parse according to the decimal system, so the result is 1; parseInt('
2', 1) -> radix is ​​1, out of range, so the result is NaN;
parseInt('3', 2) -> NaN radix is ​​2, parsed in binary system, should start with 0 and 1, so the result is NaN.

Summarize:

['1','2','3'].map(parseInt) The output is an array, the elements inside are 1, NaN, NaN respectively. The function of map is to traverse each element in the array, and perform the operation of the first function of map() on each element, here is to perform digital conversion, and the returned result is a new array. When the number of parameters passed by the function in the map is not specified, the default is to pass as many parameters as can be passed. parseInt can receive up to two parameters, so the map passes the first two parameters (current value, index value) to parseInt, followed by examples That is, parseInt('1',0) returns 0, parseInt('2',1) returns NaN, because the second parameter must be a number between 2 and 36, parseInt('3',2) What is returned is NaN, because the binary number does not include the number 3, so the result of 1, NaN, NaN will be obtained.

Guess you like

Origin blog.csdn.net/qq_51441159/article/details/128091025