One, foreword
Recently, a small partner consulted Hu Ge in the official number of this interview question, I thought it was a more interesting interview question, here to share with you small partners. I gave you the answer first, is it the same as you understand? !
[1, 2, 3].map(parseInt) // [1, NaN, NaN]Copy the code
If you know your answer, please go out and turn left: React source code/principle to learn.
Second, the analysis of
The interview question itself is not complicated. Can not answer the question of small partners, most of the focus on parseInt this function do not understand or understand the comprehensive, the following by Brother Hu for you to tell one by one.
1. The map function
The map() method creates a new array, and the result is the result returned when each element in the array calls one of the provided functions.
grammar
let newArray = arr.map(function calback(currentValue[, index[, array]]) {
// Return element for newArray
}[, thisArg])Copy the code
Callback callback function
Parameter names | meaning | optional |
---|---|---|
currentValue | The current element being processed in the callback array | Will choose |
index | The index of the current element being processed in the calback array | optional |
array | The array called by the map method | optional |
thisArg
ThisArg, optional, supports callback when the value is used as this
[1, 2, 3].map((v) => {
return v * v
})
// [1, 4, 9]Copy the code
2. The parseInt function
ParseInt (string, radix) converts a string string to an integer for radix radix, which is between 2 and 36. Return value: Returns the parsed integer value, or NaN if it cannot be converted to a value
ParseInt (10) // 10 ---- The base 10 string '10' is converted to 10 parseInt(10, 2) // 2 ---- the base 2 string '10' is converted to 2 parseInt(3, 2) // NaN -- there is no 3 in base 2Copy the code
Additional information:
If the cardinality is undefined, or the cardinality is 0, or no cardinality is specified, JavaScript does the following:
If the string string begins with “0x” or “0x”, the cardinality is 16 (hexadecimal).
If the string starts with “0” and the base is either 8 (octal) or 10 (decimal), the implementation environment determines which base. ECMAScript 5 specifies the use of 10, but not all browsers follow this rule. Therefore, the radix parameter values should always be specified.
If the string string begins with any other value, the base is 10 (decimal).
Three, witness the miracle moment
ParseInt (1, 2, 3); parseInt (1, 2, 3); map(parseInt);
[1, 2, 3]. Map ((v, index) => {return parseInt(v, index)}) [parseInt(1, 0), parseInt(2, 1), parseInt(3, 2)] ===> [1, NaN, NaN]Copy the code
Note that parseInt is written as a callback in a way that is easy to understand.
Four, conclusion
[1, 2, 3]. Map (parseInt)
Afterword.
The above is brother Hu today to share the content, like partners remember to collect, forward, click on the lower right corner of the button to see, recommended to more partners yo, welcome a lot of message exchange…
Brother Hu has words, a technology, feelings of brother Hu! Jingdong open platform chief front-end siege lion. Talk with you about the big front end, share front-end system architecture, framework implementation principles, the latest and most efficient technology practice!
Long press scan code attention, more handsome more beautiful yo! Pay attention to Brother Hu said the public number, and Brother Hu continue to in-depth exchanges!