Map、parseInt 的奇怪行为 [英] Strange behavior for Map, parseInt

查看:21
本文介绍了Map、parseInt 的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
javascript - Array.map 和 parseInt

我在 Twitter 上在推特上看到了这个奇怪的 JavaScript 行为示例

I saw this example of strange JavaScript behavior on twitter

['10','10','10','10','10'].map(parseInt)

评估为

[10, NaN, 2, 3, 4]

有人可以解释这种行为吗?我在 chrome 和 firebug 中验证过

could somebody explain this behavior? I verified it in chrome and firebug

['10','10','10','10','10'].map(function(x){return parseInt(x);})

正确地以整数形式返回一个 10s 的数组.这是对 map() 的不当使用、parseInt 的错误还是其他什么?

correctly returns an array of 10s as integers. Is this an improper use of map(), a bug with parseInt, or something else?

推荐答案

parseInt 接收两个参数:字符串和基数:

parseInt receives two arguments: string and radix:

var intValue = parseInt(string[, radix]);

while map 处理程序的第二个参数是索引:

while map handler's second argument is index:

... 使用三个参数调用回调:元素的值,元素的索引,以及被遍历的 Array 对象.

... callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

这篇关于Map、parseInt 的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆