这将用于测试一个值在位置索引是否存在,或者有更好的方法:
if(arrayName[index]==""){
// do stuff
}
这将用于测试一个值在位置索引是否存在,或者有更好的方法:
if(arrayName[index]==""){
// do stuff
}
当前回答
短而通用的方法
如果你想检查任何数组是否有假值(如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
}
其他回答
你可以使用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/
检查它是否从未被定义或是否被删除:
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确实是数组索引,请参阅检查属性名称是否为数组索引
当你创建的空数组值不是undefined时,它们是empty
var arr = new Array(10); // (10) [empty × 10]
但是当你通过索引获取item时,receive undefined
arr[0]; // undefined
所以你不能通过===比较知道它们是未定义的还是空的
我们可以使用JSON。Stringify,转换整个数组,将空值替换为null
JSON.stringify(arr); // "[null,null,null,null,null,null,null,null,null,null]"
JSON.stringify(arr[0]); // but for single item it returns undefined
对空值的实检查:
arr[0] !== null && JSON.stringify(arr.map(item => !item)).slice(1, -1).split(',')[0] === 'null'
将item与null进行比较(不应等于) 映射数组删除false代替现有值(确保字符串结构用逗号分隔,没有多余的逗号) JSON。stringify数组 从字符串中移除括号 用逗号分割成数组 与null比较
使用Lodash,你可以做到:
if(_.has(req,'documents')){
if (req.documents.length)
_.forEach(req.documents, function(document){
records.push(document);
});
} else {
}
if(_.has(req,'documents'))是检查我们的请求对象是否有一个名为documents的属性,如果它有prop,下一个if(req.documents.length)是验证它是否不是一个空数组,这样其他的东西,如forEach可以继续。