-
Notifications
You must be signed in to change notification settings - Fork 0
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
Labels
Comments
NeverWillBeEver
changed the title
['1', '2', '3'].map(parseInt) what & why ?
['1', '2', '3'].map(parseInt) 的返回值是? what & why ?
Aug 6, 2019
NeverWillBeEver
changed the title
['1', '2', '3'].map(parseInt) 的返回值是? what & why ?
【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ?
Aug 6, 2019
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
changed the title
【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ? ~JavaScript
【20190806】['1', '2', '3'].map(parseInt) 的返回值是? what & why ?
Aug 6, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
转自 Advanced-Frontend 。
整理作为笔记。侵删。
主题:JS的映射与解析
早在 2013年, 加里·伯恩哈德就在微博上发布了以下代码段:
parseInt
parseInt() 函数解析一个字符串参数,并返回一个指定基数的整数 (数学系统的基础)。
const intValue = parseInt(string[, radix]);
string: 是要被解析的值。
如果参数 string 不是一个字符串,则将其转换为字符串(使用 ToString 抽象操作)。字符串开头的空白符将会被忽略。
radix: 一个介于2和36之间的整数(数学系统的基础),表示上述字符串的基数。默认为10。
返回值: 返回一个整数或NaN
注意:
在radix为 undefined,或者radix为 0 或者没有指定的情况下,JavaScript 作如下处理:
更多详见parseInt | MDN
map
map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
更多详见Array.prototype.map() | MDN
回到真实的事例上
回到我们真实的事例上
['1', '2', '3'].map(parseInt)
对于每个迭代map, parseInt()传递两个参数: 字符串和基数。
所以实际执行的的代码是:
即返回的值分别为:
所以:
由此,加里·伯恩哈德例子也就很好解释了,这里不再赘述
如何在现实世界中做到这一点
如果您实际上想要循环访问字符串数组, 该怎么办?
map()然后把它换成数字?使用编号!
扩展
The text was updated successfully, but these errors were encountered: