['1', '2', '3'].map(parseInt) what & why ?

First of all we need to know .map () and parseInt specific use and associated parameters:

.map():

map definitions and methods 
map () method returns a new array, the array element calls a function of the value of the array element to the original processing. 
map () method in the original order of the processing elements in the array elements. 
Note: 
map will not detect an empty array 
map does not change the original array 
arr.map (function (currentValue, index,  arr), thisValue)
Parameter Description 
function (currentValue, index, arr) 
each have, function, array this function will perform the elemental function parameter 
arguments 
currentValue current element must value 
index An optional index value of the current element 
arr optional array of objects belonging to the current element.

Returns the integer obtained by the string conversion.

parseInt:
  parseInt(numString, [radix])

Parameters
numString
Required. To be converted to a string.
radix
options. Value represents the decimal number between 2 and 36. numString saved. If not provided, the prefix '0x' is treated as a hexadecimal string, prefix '0' string is treated as octal. All other strings are treated as decimal. If the parameter is less than 2 or greater than 36, then the parseInt () returns NaN.
Description
parseInt method returns an integer equal to the digital value stored in the numString. If the prefix numString not be construed as an integer, NaN (not a number) is returned.

parseInt ( "abc") // returns NaN.
parseInt ( "12abc") // returns 12.

At first glance this topic when, out of my mind the answer is [1, 2, 3], but the real answer is [1, NaN, NaN].

 

  • First, let us recall that the first parameter callback map function:

 

  var new_array = arr.map(function callback(currentValue[, index[, array]]) { // Return element for new_array }[, thisArg])

  The callback may receive a total of three parameters, wherein the first parameter representing the current element is processed, and the second parameter represents the index of the element.

 

  • ParseInt is used to parse the string, character string specified base to become an integer.
    parseInt(string, radix)
    It accepts two parameters, a first representative value (character string) to be treated, as represented by the second time base analysis.

  • After understanding these two functions, we can simulate what operation

 

  1. When parseInt ( '1', 0) // radix is ​​0, and the parameter string is not with "0x" and "0" at the beginning, in accordance with the process 10 as the base. This time returns 1
  2. parseInt ( '2', 1) // base is 1 (binary 1) numbers, the maximum value is less than 2, it can not be resolved, returns NaN
  3. parseInt number represented by (3 '', 2) // base 2 (binary), the maximum value is less than 3, it can not be resolved, returns NaN

  map function returns an array, so the final result is [1, NaN, NaN]

Comprehensive Example:

[ '10', '10', '10', '10', '10']. I (parseInt); 
// [10, 2, 3, 4]

 

Guess you like

Origin www.cnblogs.com/wangtong111/p/11202842.html