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

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

问题描述


可能存在重复:

javascript - Array.map和parseInt

我看到了这个奇怪的JavaScript行为的例子 twitter

I saw this example of strange JavaScript behavior on twitter

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

评估为

evaluates to

[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);})

正确返回10个数组作为整数。这是一个不正确的使用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 接收两个参数:string和radix:

parseInt receives two arguments: string and radix:


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

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

while map handler's second argument is index:


。 .. callback用三个参数调用:元素的值,
元素的索引以及正在遍历的Array对象。

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

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

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