Array. The prototype. The map () to understand
arr.map(functionCallback (currentValue[, index[, array]]) //> Returns a new array objectCopy the code
This callback can receive a total of three arguments,
-
The first parameter represents the element currently being processed,
-
The second parameter represents the index of the element.
-
The third parameter represents the primitive array object
ParseInt is used to parse a string, making it an integer with a specified cardinality.
parseInt(string, radix)
Copy the code
Receive two parameters
-
The first represents the value being processed (string)
-
The second said to parse the base (radix | is optional. Represents the radix of the number to be parsed. The value is between 2 and 36.) With these two functions in mind, we can simulate the operation
ParseInt (‘ 1 ‘, 0) / / radix to 0, and string parameter does not begin with “0 x” and “0”, according to the processing, having a radix of ten. // NaN parseInt(‘3’, 2); // NaN parseInt(‘3’, 2); // When radix is 2, only the values of 2 (0,1) are not in the range of 3, so it cannot be resolved. The NaN map function returns an array, so the final result is [1, NaN, NaN].
Demo test: [’11’, ’11’, ’11’,11].map(parseInt) ? Resolution:
-
ParseInt (’11’, 0); //radix is 0 and the string parameter does not start with “0x” and “0”. Return 11 at this point
-
ParseInt (’11’, 1), // For numbers represented by radix 1 (base 1), 1 is not in the scope of the radix, so it cannot be resolved, returns NaN
-
ParseInt (‘ 12 ‘, 2); // Radix = 2; parseInt(‘ 12 ‘, 2)
-
ParseInt (‘ 12 ‘, 3); // Radix = 3; parseInt(‘ 12 ‘, 3)
The map function returns an array, so the result is [11, NaN, 3, 4].
Finally, I attach a link to these two functions on MDN. The specific parameters can be found in developer.mozilla.org/zh-CN/docs/… Developer.mozilla.org/zh-CN/docs/…