This is an interesting topic I saw in the Daily- Interview-question project today. Back in 2013, Gary Bernhard posted the following code snippet on Twitter:
['10'.'10'.'10'.'10'.'10'].map(parseInt);
// [10, NaN, 2, 3, 4]
Copy the code
parseInt
The parseInt() function parses a string argument and returns an integer with a specified cardinality (the basis of a mathematical system).
const intValue = parseInt(string[, radix]);
Copy the code
String Specifies the value to be parsed. If the argument is not a string, it is converted to a string (using the ToString abstraction operation). Whitespace at the beginning of the string will be ignored.
Radix An integer between 2 and 36 (the basis of a mathematical system) that represents the radix of the above strings. The default value is 10.
Return value Returns an integer or NaN.
parseInt(100); / / 100
parseInt(100.10); / / 100
parseInt(100.2); // 4 -> converts 100 in base 2 to base 10
Copy the code
Note: when radix is undefined, or radix is 0 or not 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).
More see the parseInt | MDN
map
The map() method creates a new array. Each element in the array calls a provided function. The elements in the new array are the results returned by executing this function.
var new_array = arr.map(function callback(currentValue[,index[, array]]) {
// Return element for new_array
}[, thisArg])
Copy the code
As you can see, the callback callback takes three arguments, and we usually only use the first argument (the other two arguments are optional). CurrentValue is the current element being processed in the callback array. Index Is the optional index of the current element being processed in the callback array. Array (optional) Is the array to which the callback map method is called. Optionally, thisArg, the this value used when the callback function is executed.
const arr = [1.2.3];
arr.map((num) = > num + 1); / / [2, 3, 4]
Copy the code
More can be found in the Array. The prototype. The map () | MDN
Let’s go back to the real example
Let’s go back to our real example
['1'.'2'.'3'].map(parseInt)
Copy the code
For each iteration map, parseInt() passes two arguments: a string and a cardinality. So the actual code executed is:
['1'.'2'.'3'].map((item, index) = > {
return parseInt(item, index)
})
Copy the code
That is, the returned values are:
parseInt('1'.0) / / 1
parseInt('2'.1) // NaN, radix needs to be between 2 and 36
parseInt('3'.2) // NaN, 3 is not binary
Copy the code
So:
['1'.'2'.'3'].map(parseInt)
// [1, NaN, NaN]
Copy the code
So the Gary Bernhard example is easy to explain, and I won’t repeat it here
['10'.'10'.'10'.'10'.'10'].map(parseInt);
// [10, NaN, 2, 3, 4]
Copy the code
How do you do that in the real world
What if you just want to loop through an array of strings? When you use map(), you just change it to a number.
['10'.'10'.'10'.'10'.'10'].map(Number);
// [10, 10, 10, 10, 10]
Copy the code
series
- JS series 1: var, let, const, deconstruction, expansion, function
For more on this series,Click on the Github homepage
Walking in the final
1. ❤️ Have fun, keep learning, and always keep coding. 👨 💻
2. If you have any questions or more unique insights, please feel free to comment or contact Me directly (123)! 👀 👇
3. 👇 Welcome to pay attention to: front-end bottle Jun, daily update! 👇