start
-
look at a topic first
['1','2','3'].map(parseInt)
-
At first glance, I write the answer:
[1,2,3]
-
Console run:
['1',NaN,NaN]
-
Why? I am familiar with the map function, so where is the problem? There is a high probability that it is
parseInt
on
review the map
map()
method creates a new array consisting of the values returned by calling the provided function once for each element in the original array. There are only two parameters in total, one is the callback function, and the other is to specify the corresponding this.
1.第一个参数:回调函数
var newArr = [1, 2, 3].map((item, index, arr) => {
console.log(item, index, arr)
return item * 2
})
/*
1 0 [ 1, 2, 3 ]
2 1 [ 1, 2, 3 ]
3 2 [ 1, 2, 3 ]
*/
console.log(newArr) // [ 2, 4, 6 ]
The three parameters of the callback function are: 1. The element currently being processed; 2. The index of the current element; 3. The array calling the map;
2.第二个参数:指定this
var obj = {
name: 'obj对象' }
var newArr = [1, 2, 3].map(function (item, index, arr) {
return item * 2 + this.name
}, obj)
console.log(newArr) // [ '2obj对象', '4obj对象', '6obj对象' ]
Note here that the form of an arrow function cannot be used here, because the this of the arrow function does not have its own this object, and the internal this is the this in the upper scope when it is defined.
Learn about parseInt
parseInt(string, radix) parses a string and returns a decimal integer of the specified radix, which radix
is an integer between 2-36, indicating the radix of the parsed string.
How to understand:
- Convert a string A to a number B;
- The first parameter: the string A to be converted;
- The second parameter: indicates the base number of the base system (in my own vernacular: when parsing the string A, treat A as a number in base system.)
Normal use:
console.log(parseInt('10')) // 10
console.log(parseInt('10a')) // 10
console.log(parseInt('111', 2)) // 7
console.log(parseInt('A12', 16)) // 2578
Precautions:
For parseInt first parameter. If the argument is not a string, it is converted to a string (using ToString
the abstract operation). Whitespace at the beginning of the string will be ignored.
For parseInt second parameter. An integer from 2
to that 36
represents the base of the base. For example, specifying 16
indicates that the parsed value is a hexadecimal number. Will return if outside of this range NaN
. If specified 0
or not specified, the radix will be inferred from the value of the string. Note that the extrapolated result will not always be the default value 10
! The description at the end of the article explains the specific behavior of the function when the parameter radix
is not passed.
1. If the first parameter is not a string, it will be converted to a string, and the leading blank character will be ignored.
console.log(parseInt(10)) // 10
console.log(parseInt(' 10')) // 10
console.log(parseInt(' 11 22')) // 11 /* 如果字符之间有空格,读取第一个 */
2. The second parameter is an integer with a value range of 2-36 . If it exceeds this range, it will be returned NaN
.
console.log(parseInt('10', 1)) // NaN
console.log(parseInt('10', 2)) // 2
console.log(parseInt('10', 36)) // 36
console.log(parseInt('10', 37)) // NaN
3. If the second parameter is specified 0
or not specified, the base will be calculated based on the value of the string.
console.log(parseInt('10')) // 10
console.log(parseInt('010')) // 10 /* 某些浏览器这里会解读成 8进制 */
console.log(parseInt('ox10')) // NaN
/* 第二个参数如果是字符串,会被尝试读取数字 */
console.log(parseInt('10', '36')) // 36
/* **我实践的结果:** 第二个参数如果是字符串,会被尝试读取数字,无法读取,默认基数将会根据字符串的值进行推算*/
console.log(parseInt('10', '7tamato')) // 10
console.log(parseInt('10', 'tamato7')) // 10
console.log(parseInt('0x10', 'tamato7')) // 16
Estimated by:
- If the input
string
starts with0x
or0X
(a 0 followed by a lowercase or uppercase X), then the radix is assumed to be 16 and the rest of the string is parsed as a hexadecimal number.- If the
string
input0
starts with " " (0),radix
it is assumed to be8
(octal) or10
(decimal). Which radix to choose is implementation dependent. ECMAScript 5 clarifies that 10 (decimal) should be used, but not all browsers support it. Therefore,parseInt
when , be sure to specify a radix .- Yes (decimal) if the entered starts
string
with any other value.radix
10
4. The parsed string contains numbers exceeding the cardinality, returning NaN
console.log(parseInt('3', 2)) // NaN
console.log(parseInt('4', 3)) // NaN
console.log(parseInt('1324', 3)) // NaN
5. More than 7 digits after the decimal point
console.log(parseInt(0.000005)) // 0
console.log(parseInt(0.0000005)) // 5
/* 原因是 0.0000005 不是字符串,会被toString() => 5e-7 */
console.log((0.0000005).toString()) // 5e-7
back to the original question
['1','2','3'].map(parseInt)
It can be understood as this:
parseInt('1',0,['1','2','3'])
parseInt('2',1,['1','2','3'])
parseInt('3',2,['1','2','3'])
-
parseInt does not have a third parameter, so it can be ignored.
-
base is 0 => default decimal
-
Parsed string contains digits beyond base, returns NaN
So the answer:[1,NaN,NaN]
Summarize:
-
Since the calculation of the second parameter of parseInt is not stable, it is recommended to add the second parameter when using parseInt.
-
Let's take a look at the parsing rules:
- The first parameter defaults to a string type, not a string type, and will be ignored
toString
; if the string cannot be parsed, NaN will be returned directly; - If the second parameter is specified as 0 or not specified, it will be inferred according to the format of the first parameter. The value range of the cardinality is 2-36; the cardinality is out of range NaN;
- The first parameter defaults to a string type, not a string type, and will be ignored
come all come
It’s all here, let’s take a look at the ones that parseInt
are similar to parseFloat
, parseFloat
relatively simple, with only one parameter, which is the string to be parsed.
For data of type BigInt, use parseInt and parseFloat to convert data of type BigInt, there will be loss of precision, you need to pay attention.
end
- In fact, this question is to examine the degree of familiarity with
parseInt
the method . come on