Javascript 在 jQuery 中检查数组是否为空的最佳程序是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44277505/
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
What is the best procedure to check an array is empty or not in jQuery?
提问by Mohibul Hasan Rana
I am using like to check if array is not empty
我正在使用 like 检查数组是否为空
if(array != null){
//code
}
I also found like that
我也发现这样
if(Array.isArray(array)){
//code
}
and
和
if(array.length){
//code
}
Which one is better to use above three ?
以上三种用哪一种比较好?
回答by Nina Scholz
I suggest to use Array.isArrayand the lengthproperty of the array
我建议使用Array.isArray和数组的length属性
if (Array.isArray(array) && array.length) {
// code
}
because it checks if arrayis an array and if the length has a truthy value.
因为它检查是否array是一个数组以及长度是否具有真值。
Comparing your attempts:
比较您的尝试:
Truthy check
if (array != null) { // which is basically the same as if (array) { //code }This is
truefor all truthy values, like1,'a',{}. This result is not wanted.Array check
if (Array.isArray(array)) { // code }This checks only if
arrayis an array, but not the length of it. Empty arrays returnstrue, which is not wanted.Length check
if (array.length) { // code }This works only for objects which may have a property of length, which have a truthy value.
While this comes close to the wanted result, it might be wrong, for example with objects like
{ length: 'foo' }or with array-like objects.
真实检查
if (array != null) { // which is basically the same as if (array) { //code }这适用
true于所有真实值,例如1,'a',{}。这个结果是不想要的。数组检查
if (Array.isArray(array)) { // code }这只检查是否
array是一个数组,而不是它的长度。空数组返回true,这是不想要的。长度检查
if (array.length) { // code }这仅适用于可能具有长度属性且具有真值的对象。
虽然这接近于想要的结果,但它可能是错误的,例如对于像这样的对象
{ length: 'foo' }或使用类似数组的对象。
回答by philipp
If you want to know if an Object is an Array, then Array.isArray()will do.
如果你想知道一个对象是否是一个数组,那么Array.isArray()就可以了。
If you want to know if an Array has items, than array.length !== 0will do.
如果你想知道一个 Array 是否有项目,那么array.length !== 0就可以了。
If you want to know if a variable is not null than array !== nullwill do.
如果你想知道一个变量是否不为空,那么array !== null就可以了。
回答by Deep Mistry
It's better to check the length of the array but there is one issue in it. Consider if someone entered an undefined value in the array then the array will be empty but still have the length greater than 0.
最好检查数组的长度,但其中存在一个问题。考虑如果有人在数组中输入了未定义的值,则数组将为空,但长度仍大于 0。
for(var i=0,i<array.length;i++){
if(typeof(array[i]) !== 'undefined'){
};
};
回答by gabriel
I am use to array.lengthTo check if array is not empty, use if( array.length > 0) {}
我习惯于array.length检查数组是否为空,使用if( array.length > 0) {}
回答by bugs_cena
var isEmptyArray = function(arr) {
return (arr || []).length === 0;
}
var arr1 = [1, 2, 3];
var arr2 = [];
var arr3 = undefined;
console.log(isEmptyArray(arr1)); // false
console.log(isEmptyArray(arr2)); // true
console.log(isEmptyArray(arr3)); // true
回答by Raising Spirit
the best solution is
最好的解决办法是
if (array.length>0){
....
}
but this sentence have a problem if you plan in deleting indexes from the array, since you can have an array like this [undefined,undefined,undefined], whos length is 3 but is technically empty.
但是如果你打算从数组中删除索引,这句话就会有问题,因为你可以有一个像 [undefined,undefined,undefined] 这样的数组,它的长度是 3 但技术上是空的。
var a = [1,2,3];
delete a[0];
delete a[1];
delete a[2];
a.length > 0 /// true
a /// [undefined x 3]
Take this in consideration to make the exact sentence
考虑到这一点,使准确的句子

