我现在如何检查该值是否定义为未定义,或者它是否真的未定义?
例如。
var a = [];
a[0] = undefined;
// a defined value that's undefined
typeof a[0] === "undefined";
// and a value that hasn't been defined at all
typeof a[1] === "undefined";
有没有办法把这两者分开?可以使用 for-in 循环遍历数组,但是有没有更简单的方法?
最佳答案
您可以检查索引是否在给定数组中:
0 in a // => true
1 in a // => false
关于Javascript : separating array defined value that's undefined and array value that's not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10581353/