这将用于测试一个值在位置索引是否存在,或者有更好的方法:
if(arrayName[index]==""){
// do stuff
}
这将用于测试一个值在位置索引是否存在,或者有更好的方法:
if(arrayName[index]==""){
// do stuff
}
当前回答
我建议创建一个这样的函数:
function isEmptyEl(array, i) {
return !(array[i]);
}
你可以这样称呼它:
if (isEmptyEl(arrayName, indexVal)) {
console.log('arrayName[' + indexVal + '] is empty');
}
强制开发人员使用isemptyl接口将捕获输入错误,例如未定义的arrayName或indexVal变量。
(在使用Javascript编程时,防御性编程通常是一个很好的实践。)
如果没有定义arrayName,就会抛出这样的错误:
Uncaught ReferenceError: arrayName is not defined
at <anonymous>:2:15
at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
at Object.InjectedScript.evaluate (<anonymous>:694:21)
未定义的indexVal也有类似的结果。
如果数组或索引值不存在,就会得到一个错误。
对于有效的输入,如果arrayName[indexVal]是以下任意一个,你只会得到一个true:
零 未定义的 南 空字符串 0 假
其他回答
短而通用的方法
如果你想检查任何数组是否有假值(如false, undefined, null或空字符串),你可以像这样使用every()方法:
array.every(function(element) {return !!element;}); // returns true or false
例如:
['23', null, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false
['23', '', 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false
['23', true, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns true
如果你需要得到一个假值的第一个索引,你可以这样做:
let falsyIndex;
if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
console.log(falsyIndex);
} // logs 3
如果你只需要检查一个给定索引的数组的假值,你可以这样做:
if (!!array[index]) {
// array[index] is a correct value
}
else {
// array[index] is a falsy value
}
检查它是否从未被定义或是否被删除:
if(typeof arrayName[index]==="undefined"){
//the index is not in the array
}
也适用于关联数组和删除某些索引的数组
要检查它是否从未被定义,was deleted OR是一个空值或逻辑空值(NaN,空字符串,false):
if(typeof arrayName[index]==="undefined"||arrayName[index]){
//the index is not defined or the value an empty value
}
这取决于你对“空”的定义。
当您试图获取一个对象的属性值时,该对象没有该名称的属性,您将获得该值为undefined。
这就是稀疏数组的情况:不是所有的索引都在0和数组之间。长度为1的存在。
所以你可以检查array[index] === undefined。
但是,属性索引可能存在一个未定义的值。如果你想过滤掉这种情况,你可以使用in操作符或hasOwnProperty,如如何检查一个对象在JavaScript中是否有属性所述?
index in array;
array.hasOwnProperty(index);
如果你想要考虑一个具有未定义或空值的现有属性不存在,你可以使用松散比较数组[index] == undefined或数组[index] == null。
如果你知道数组不是稀疏的,你可以比较index和array.length。但为了安全起见,您可能希望确保index确实是数组索引,请参阅检查属性名称是否为数组索引
如果数组[index]为空,请尝试此操作
if (array[index] != null)
从概念上讲,JavaScript中的数组包含数组。长度元素,从数组[0]开始,直到数组[array. array.]长度- 1]。如果i在0和array之间,则索引为i的数组元素定义为数组的一部分。长度-包括1。如果i不在这个范围内,它就不在数组中。
因此,从概念上讲,数组是线性的,从0开始,一直到最大值,没有任何机制在这个范围内存在没有条目的“间隙”。要找出一个值是否存在于给定的位置索引(其中索引为0或正整数),您只需使用
if (i >= 0 && i < array.length) {
// it is in array
}
Now, under the hood, JavaScript engines almost certainly won't allocate array space linearly and contiguously like this, as it wouldn't make much sense in a dynamic language and it would be inefficient for certain code. They're probably hash tables or some hybrid mixture of strategies, and undefined ranges of the array probably aren't allocated their own memory. Nonetheless, JavaScript the language wants to present arrays of array.length n as having n members and they are named 0 to n - 1, and anything in this range is part of the array.
然而,您可能想要知道数组中的值是否实际上是有定义的—也就是说,它不是未定义的。也许你甚至想知道它是否有定义而不是null。可以在不设置数组值的情况下向数组添加成员:例如,如果您通过增加数组来添加数组值。属性,任何新值将是未定义的。
确定给定的值是否有意义,或者是否已定义。即非undefined或null:
if (typeof array[index] !== 'undefined') {
or
if (typeof array[index] !== 'undefined' && array[index] !== null) {
有趣的是,由于JavaScript的比较规则,我的最后一个例子可以优化为:
if (array[index] != null) {
// The == and != operators consider null equal to only null or undefined
}