Javascript 检查数组是否有空元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36622064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check the array has empty element or not
提问by Juntae
How can I check the array has empty element or not? Imagine this array,
如何检查数组是否有空元素?想象一下这个数组,
var arr = [ 'a', 'b', , 'd'];
the arr[2] is undefined
. I want to check this. If the element has empty element, return 'true' or return false. Maybe like this,
arr[2] 是undefined
. 我想检查一下。如果元素有空元素,则返回 'true' 或返回 false。也许像这样,
function hasEmptyElement(array){
for (var i=0; i<array.length; i++){
if (typeof arr[i] == 'undefined'){
return true;
// and then ?
// should I use double for loop or helper variable?
}
}
}
I confuse how can I do this. Please help me the clevers.
我很困惑我怎么能做到这一点。请聪明人帮帮我。
回答by Chiru
As of ES2016, you should use Array.prototype.includes:
从 ES2016 开始,您应该使用Array.prototype.includes:
const array = ["a", "b", , "d"];
array.includes(undefined); // true
(You don't need to write undefined
, but this makes it more clear what's happening.)
(您不需要写undefined
,但这可以更清楚地说明正在发生的事情。)
Note that this method treats slots valued undefined
and emptyslots the same, although they're not. If you need to differentiate these two cases as well, starting from ES2017, you can use Object.valuesmaking the following expression true
if there are empty slots in the array:
请注意,此方法将插槽值undefined
和空插槽视为相同,尽管它们不是。如果您还需要区分这两种情况,从 ES2017 开始,如果数组中有空槽,您可以使用Object.values制作以下表达式true
:
Object.values(array).length === array.length; // true
回答by Oriol
First, note the difference between empty slots and slots with undefined value:
首先,注意空插槽和具有未定义值的插槽之间的区别:
var arr = [/*empty slot*/, undefined];
Object.keys(arr); // ["1"] but not "0"
"0" in arr; // false
"1" in arr; // true
ES5 array methods skip empty slots. ES6 [].includes
does not.
ES5 数组方法跳过空槽。ES6[].includes
没有。
That means you can use
这意味着你可以使用
arr.includes(undefined); // has empty slot OR contains undefined value
arr.indexOf(undefined) > -1; // contains undefined value
If you want to test only if there are empty slots, you can iterate manually with a for
loop and check whether all indices between 0 and the length of the array are present, e.g. with in
operator.
如果您只想测试是否有空槽,您可以使用循环手动迭代for
并检查是否存在 0 和数组长度之间的所有索引,例如使用in
运算符。
(function() {
for(var i=0; i<arr.length; ++i) if(!(i in arr)) return true;
return false;
})(); // has empty slot
Or you can also use ES5 array methods and check if they skipped an index.
或者您也可以使用 ES5 数组方法并检查它们是否跳过了索引。
var n = 0;
arr.some((_,i) => i !== n++); // has empty slot
回答by yaswanthkoneri
Simple implementation using set
使用 set 的简单实现
var arr = [ 'a', 'b', , 'd'];
// Using Set from es6
var arraySet = new Set(arr)
arraySet.has(undefined) // returns true
Thanks!
谢谢!
回答by Nikolai Mavrenkov
You can do something like that:
你可以这样做:
function hasEmptyElement(array){
for (var i=0; i<array.length; i++){
if (!(i in array)) {
return true;
}
}
return false;
}
The main point of this implementation is that it makes difference between [1,,3]
and [1, undefined, 3]
这个实现的要点是它在[1,,3]
和[1, undefined, 3]
回答by Trish
var temp = arr.filter(item => item);
This will give you a new array with below elements:
["a", "b", "d"]
这将为您提供一个包含以下元素的新数组:
["a", "b", "d"]
The above solution will help to remove empty as well as null values.
上述解决方案将有助于删除空值和空值。
回答by isvforall
You could also use Array.prototype.findIndex()
你也可以使用 Array.prototype.findIndex()
var arr = ['a', 'b', , 'd'];
document.write(arr.findIndex(e => e === undefined) > -1);
回答by Mujahid
`Check null/undefined values in Array`
function checkNullValue(a) {
if (typeof (a) == 'undefined' || a === null) {
return false;
} else {
return true;
}
}
var arrayMonthVal = ['1','2',,'6','null', '8'];
var pass = arrayMonthVal.some(checkNullValue);
回答by gurvinder372
try
尝试
var hasAnyEmptyElement = arr.filter(function(val){ return (typeof val) != "undefined" }).length != arr.length;
DEMO
演示
var arr = [1,2];
arr[4] = 2;
var hasAnyEmptyElement = arr.filter(function(val){ return (typeof val) != "undefined" }).length != arr.length;
alert(hasAnyEmptyElement);
回答by Rahul Tripathi
You can try like this:
你可以这样试试:
var arr = [ 'a', 'b',, 'd'];
function myfunc(arr) {
for(var i=0; i<arr.length; i++) {
if (!(i in arr)) return false;
}
return true;
}
alert( myfunc(arr));