javascript - 为什么一个只有一个数字的数组在 Javascript 中是一个数字?

标签 javascript arrays nan

我发现一个空数组或一个只有一个数的数组是一个数。

这个题目不是真的解释这个特例我觉得:Why does isNaN(" ") equal false

document.write( isNaN([1,2,3]) ); // true
document.write( isNaN([1,2,'abc']) ); // true
document.write( isNaN(['abc']) ); // true

// maybe explained through the above link
document.write( isNaN([]) ); // false

// but...
document.write( isNaN([1]) ); // false
document.write( isNaN([-3]) ); // false
document.write( isNaN([1234567]) ); // false
document.write( isNaN([-1.234]) ); // false
document.write( isNaN([[123]]) ); // false
document.write( isNaN(['1']) ); // false

谁能告诉我为什么有道理?

最佳答案

isNaN 将其值强制转换为数字。 ( See MDN )

因为数组的字符串表示是用逗号连接的所有项目。因为有逗号,所以它的数字表示是 NaN

但如果只有一项,因此没有逗号,则可以将其转换为数字。

关于javascript - 为什么一个只有一个数字的数组在 Javascript 中是一个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27105631/

相关文章:

python - 检查 pandas 中的 'None' 值时排除 'NaN'

python - 在管道中使用 Imputer 不会删除 NaN,给出 '' 输入包含 NaN、无穷大或对于 dtype 来说太大的值 ('float64' )''

javascript - Mongoose 模式无法读取未定义的属性 'password'

javascript - 在javascript中的几个假类中公开字符串全局列表的简单方法

javascript - d3.js - 强制布局和节点位置

java - 我如何遍历这个数组列表

javascript - null 是否被视为零和 undefined 而不是算术表达式中的数字?

javascript - 如何在 react 中一项一项地渲染列表中的项目?

javascript - 如何防止history.back()返回登录页面

arrays - 在 Swift 2.2 (3.0) 中映射修改对象数组