这将用于测试一个值在位置索引是否存在,或者有更好的方法:

if(arrayName[index]==""){
     // do stuff
}

当前回答

我想指出一些似乎已经错过的东西:即有可能在数组的中间有一个“空”数组位置。考虑以下几点:

let arr = [0, 1, 2, 3, 4, 5]

delete arr[3]

console.log(arr)      // [0, 1, 2, empty, 4, 5]

console.log(arr[3])   // undefined

自然的检查方法是查看数组成员是否未定义,我不确定是否存在其他方法

if (arr[index] === undefined) {
  // member does not exist
}

其他回答

我们就不能这样做吗

if(arrayName.length > 0){   
    //or **if(arrayName.length)**
    //this array is not empty 
}else{
   //this array is empty
}

如果数组[index]为空,请尝试此操作

if (array[index] != null) 

短而通用的方法

如果你想检查任何数组是否有假值(如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
}

从概念上讲,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
}  

好的,让我们先看看如果JavaScript中不存在数组值会发生什么,所以如果我们有一个如下所示的数组:

const arr = [1, 2, 3, 4, 5];

现在我们检查6是否在下标5处:

arr[5];

我们得到 未定义……

这基本上给了我们答案,检查是否未定义的最好方法,像这样:

if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}

在这种情况下最好不要这样做:

if(!arrayName[index]) {
  //Don't check like this..
}

因为假设我们有这样一个数组:

const arr = [0, 1, 2];

我们这样做:

if(!arr[0]) {
  //This get passed, because in JavaScript 0 is falsy
}

正如你所看到的,即使0在那里,它也不会被识别,还有一些其他的事情可以做同样的事情,让你的应用程序有bug,所以要小心,我把它们都列了下来:

Undefined:如果该值没有定义并且是未定义的 null:如果它是空的,例如如果一个DOM元素不存在… 空字符串:" 0:数字0 南:不是数字 假