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

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

当前回答

if(typeof arr ==='object' && arr instanceof Array ){
   if(!arr.length){
      println 'empty'
   }else{
      printn 'not Empty'
   }

}else{
   println 'Null'
}

如果你的意思是'Null' ->它的元素为Null或等于",在这种情况下:在过滤所有'Null'元素后检查数组是否为空

if(!arr.clean().length){return 'is null'}

当然,之前的Add Clean方法:

Array.prototype.clean=function(){return this.filter(function(e){return (typeof  e !=='undefined')&&(e!= null)&&(e!='')})}

其他回答

if(arrayName.length > index && arrayName[index] !== null) {
    //arrayName[index] has a value
}

短而通用的方法

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

实检测:在运算符中

这个问题的年龄大约是10年,令人惊讶的是,还没有人提到这一点-然而,有些人看到了当我们使用删除操作符(例如这里)时的问题。这也是一个有点违反直觉的解决方案,但在“对象世界”中工作的in操作符也可以用于数组(因为我们可以像在“键”上一样查看数组索引……)通过这种方式,我们可以检测并区分未定义的数组值和被delete删除的值(索引)

if(index in arrayName) {
   // do stuff 
}

let arr = [0, 1, 2, 3, null, undefined,6] delete arr[2]; // we delete element at index=2 if(2 in arr) console.log('You will not see this because idx 2 was deleted'); if(5 in arr) console.log('This is element arr[5]:', arr[5]); // Whole array and indexes bigger than arr.length: for(let i=0; i<=9; i++) { let val = (i in arr) ? arr[i] : 'empty' let bound = i<arr.length ? '' : '(out of range)' console.log(`${i} value: `, val, bound); } console.log('Look on below aray on chrome console (not in SO snippet console)'); console.log('typeof arr:', typeof arr); console.log(arr);

Chrome控制台显示了一些关于删除索引2的代码片段数组的信息-这个索引实际上根本不存在(!!)(就像从对象中删除键一样)。有趣的是,数组被视为键值对(我们甚至看到'length'键)。同样有趣的是,typeof arr是Object (!!), delete和in操作符的工作方式类似于JS对象 (同样方括号符号arr[idx]和obj[key]是类似的)-所以它看起来像数组是一些特殊的JS对象在核心。

为了得到类似的效果而不删除定义数组如下

[0, 1,, 3, null, undefined, 6] // pay attention to double comma: ",,"

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

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
}

你可以使用Loadsh库来更有效地做到这一点,比如:

如果你有一个名为“pets”的数组,例如:

var pets = ['dog', undefined, 'cat', null];

console.log(_.isEmpty(pets[1])); // true
console.log(_.isEmpty(pets[3])); // true
console.log(_.isEmpty(pets[4])); // false

_.map( pets, (pet, index) => { console.log(index + ': ' + _.isEmpty(pet) ) });

检查所有数组值是否为空或未定义的值:

Var pets = ['dog', undefined, 'cat', null]; 宠物console.log (_.isEmpty ([1]));/ /正确的 console.log (_.isEmpty(宠物[3]));/ /正确的 console.log (_.isEmpty(宠物[4]));/ /错误 _。地图(宠物,宠物,指数= > {console.log(指数 + ': ' + _. isEmpty (pet))}); < script src = " https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js " > < /脚本>

更多例子请访问http://underscorejs.org/