Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ? #1

Open
NeverWillBeEver opened this issue Aug 6, 2019 · 0 comments
Assignees

Comments

@NeverWillBeEver
Copy link
Owner

转自 Advanced-Frontend
整理作为笔记。侵删。

主题:JS的映射与解析

早在 2013年, 加里·伯恩哈德就在微博上发布了以下代码段:

['10','10','10','10','10'].map(parseInt);
// [10, NaN, 2, 3, 4]

parseInt
parseInt() 函数解析一个字符串参数,并返回一个指定基数的整数 (数学系统的基础)。

const intValue = parseInt(string[, radix]);
string: 是要被解析的值。
如果参数 string 不是一个字符串,则将其转换为字符串(使用 ToString 抽象操作)。字符串开头的空白符将会被忽略。

radix: 一个介于2和36之间的整数(数学系统的基础),表示上述字符串的基数。默认为10。
返回值: 返回一个整数或NaN

parseInt(100); // 100
parseInt(100, 10); // 100
parseInt(100, 2); // 4 -> converts 100 in base 2 to base 10 // 用十进制来表示二进制的 100

注意:
在radix为 undefined,或者radix为 0 或者没有指定的情况下,JavaScript 作如下处理:

如果字符串 string 以"0x"或者"0X"开头, 则基数是16 (16进制).
如果字符串 string 以"0"开头, 基数是8(八进制)或者10(十进制),那么具体是哪个基数由实现环境决定。ECMAScript 5 规定使用10,但是并不是所有的浏览器都遵循这个规定。因此,永远都要明确给出radix参数的值。
如果字符串 string 以其它任何值开头,则基数是10 (十进制)。

更多详见parseInt | MDN

map
map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。

var new_array = arr.map(function callback(currentValue[,index[, array]]) {
 // Return element for new_array
 }[, thisArg])

可以看到callback回调函数需要三个参数, 我们通常只使用第一个参数 (其他两个参数是可选的)。
currentValue 是callback 数组中正在处理的当前元素。
index可选, 是callback 数组中正在处理的当前元素的索引。
array可选, 是callback map 方法被调用的数组。
另外还有thisArg可选, 执行 callback 函数时使用的this 值。

const arr = [1, 2, 3];
arr.map((num) => num + 1); // [2, 3, 4]

更多详见Array.prototype.map() | MDN

回到真实的事例上
回到我们真实的事例上

['1', '2', '3'].map(parseInt)
对于每个迭代map, parseInt()传递两个参数: 字符串和基数。
所以实际执行的的代码是:

['1', '2', '3'].map((item, index) => {
	return parseInt(item, index)
})

即返回的值分别为:

parseInt('1', 0) // 1
parseInt('2', 1) // NaN
parseInt('3', 2) // NaN, 3 不是二进制

所以:

['1', '2', '3'].map(parseInt)
// 1, NaN, NaN

由此,加里·伯恩哈德例子也就很好解释了,这里不再赘述

['10','10','10','10','10'].map(parseInt);
// [10, NaN, 2, 3, 4]

如何在现实世界中做到这一点
如果您实际上想要循环访问字符串数组, 该怎么办?
map()然后把它换成数字?使用编号!

['10','10','10','10','10'].map(Number);
// [10, 10, 10, 10, 10]

扩展

['2.5','-2','e','0a','a','0x11','NaN', '', 'Infinity'].map(Number);
// [2.5, -2, NaN, NaN, NaN, 17, NaN, 0, Infinity]

['20','-2','e','0a','a','0x11','NaN', '', 'Infinity'].map(Math.cos);
//  [0.40808206181339196, -0.4161468365471424, NaN, NaN, NaN, -0.27516333805159693, NaN, 1, NaN]
@NeverWillBeEver NeverWillBeEver changed the title ['1', '2', '3'].map(parseInt) what & why ? ['1', '2', '3'].map(parseInt) 的返回值是? what & why ? Aug 6, 2019
@NeverWillBeEver NeverWillBeEver changed the title ['1', '2', '3'].map(parseInt) 的返回值是? what & why ? 【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ? Aug 6, 2019
@NeverWillBeEver NeverWillBeEver changed the title 【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ? 【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ? ~JavaScript Aug 6, 2019
@NeverWillBeEver NeverWillBeEver changed the title 【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ? ~JavaScript 【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ? Aug 6, 2019
@NeverWillBeEver NeverWillBeEver self-assigned this Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant