array.map(parseInt)

['1', '2', '3']. Map (parseInt) What
this output is a very remote knowledge point, it will cause you to be surprised every time you read it, but after reading it, it is very simple , After a while, I forgot to get rid of everything, so I decided to write a blog to remind myself:
output: [1, NaN, NaN].
First let us review, the first parameter callback of the map function:

Insert picture description here
then parseInt function
Insert picture description here
The parameters received by these two functions are shown above.
After understanding these two functions, we can simulate the operation

parseInt ('1', 0) // When radix is ​​0, and the string parameter does not start with "0x" and "0", the base 10 is used. At this time, return 1
parseInt ('2', 1) // In the number represented by 1 (1 base), the maximum value is less than 2, so it cannot be parsed, and return NaN
parseInt ('3', 2) // The base is Among the numbers represented by 2 (binary), the maximum value is less than 3, so it cannot be parsed. The NaN
map function returns an array, so the final result is [1, NaN, NaN]

This is the explanation of these two functions by MDN and W3School.

Published 20 original articles · Likes5 · Visits 2070

Guess you like

Origin blog.csdn.net/qq_42859887/article/details/105483542