检查数组是否为空或空

时间:2021-08-12 11:13:33

I would like to know how to check if an array is empty or null in jQuery. I tried array.length === 0 but it didn't work. It did not throw any error either.

我想知道如何检查jQuery中的数组是否为空或空。我试着数组。长度=== 0,但不管用。它也没有抛出任何错误。

This is the code:

这是代码:

var album_text = new Array();

$("input[name='album_text[]']").each(function(){
  if( $(this).val() &&  $(this).val() != '') {
    album_text.push($(this).val());
  }
});
if (album_text.length === 0) {
  $('#error_message').html("Error");
}

else {
  // send data
}

4 个解决方案

#1


124  

As long as your selector is actually working, I see nothing wrong with your code that checks the length of the array. That should do what you want. There are a lot of ways to clean up your code to be simpler and more readable. Here's a cleaned up version with notes about what I cleaned up.

只要您的选择器确实在工作,那么检查数组长度的代码就没有问题。你想怎么做就怎么做。有很多方法可以清理代码,使其更简单、更可读。这是一个清理版,上面有我清理过的东西的注释。

var album_text = [];

$("input[name='album_text[]']").each(function() {
    var value = $(this).val();
    if (value) {
        album_text.push(value);
    }
});
if (album_text.length === 0) {
    $('#error_message').html("Error");
}

else {
  //send data
}

Some notes on what you were doing and what I changed.

一些关于你在做什么和我改变了什么的笔记。

  1. $(this) is always a valid jQuery object so there's no reason to ever check if ($(this)). It may not have any DOM objects inside it, but you can check that with $(this).length if you need to, but that is not necessary here because the .each() loop wouldn't run if there were no items so $(this) inside your .each() loop will always be something.
  2. $(this)始终是一个有效的jQuery对象,因此没有理由检查是否($(this)。它可能没有任何DOM对象,但是您可以使用$(this)检查它。如果需要的话,可以使用length,但是这里没有必要,因为如果没有项,.each()循环就不会运行,所以.each()循环中的$(this)将始终是一些东西。
  3. It's inefficient to use $(this) multiple times in the same function. Much better to get it once into a local variable and then use it from that local variable.
  4. 在同一个函数中多次使用$(this)是低效的。最好是将它一次放入局部变量,然后从该局部变量中使用它。
  5. It's recommended to initialize arrays with [] rather than new Array().
  6. 建议使用[]初始化数组,而不是使用new Array()。
  7. if (value) when value is expected to be a string will both protect from value == null, value == undefined and value == "" so you don't have to do if (value && (value != "")). You can just do: if (value) to check for all three empty conditions.
  8. 如果(value)当值被期望为一个字符串时,它将同时保护值== null, value ==未定义和值== ",所以你不必做if (value && (value != ""))。您可以这样做:if (value)检查所有三个空条件。
  9. if (album_text.length === 0) will tell you if the array is empty as long as it is a valid, initialized array (which it is here).
  10. 如果(album_text。length === = 0)将告诉您数组是否为空,只要它是一个有效的初始化数组(它在这里)。

What are you trying to do with this selector $("input[name='album_text[]']")?

您正在尝试使用这个选择器$(“input[name='album_text[]']]”)做什么?

#2


59  

User JQuery is EmptyObject to check whether array is contains elements or not.

用户JQuery是EmptyObject,用于检查数组是否包含元素。

var testArray=[1,2,3,4,5];
var testArray1=[];
console.log(jQuery.isEmptyObject(testArray)); //false
console.log(jQuery.isEmptyObject(testArray1)); //true

#3


6  

You should check for '' (empty string) before pushing into your array. Your array has elements that are empty strings. Then your album_text.length === 0 will work just fine.

在进入数组之前,应该检查“(空字符串)”。数组中的元素都是空字符串。那么你的album_text。长度=== 0就可以了。

#4


6  

I think it is dangerous to use $.isEmptyObject from jquery to check whether the array is empty, as @jesenko mentioned. I just met that problem.

我认为使用美元是危险的。isEmptyObject来自jquery,用于检查数组是否为空,如@jesenko所述。我刚遇到这个问题。

In the isEmptyObject doc, it mentions:

在isEmptyObject doc中,它提到:

The argument should always be a plain JavaScript Object

参数应该始终是一个普通的JavaScript对象

which you can determine by $.isPlainObject. The return of $.isPlainObject([]) is false.

可以通过$. isplainobject来确定。$. isplainobject([])返回值为false。

#1


124  

As long as your selector is actually working, I see nothing wrong with your code that checks the length of the array. That should do what you want. There are a lot of ways to clean up your code to be simpler and more readable. Here's a cleaned up version with notes about what I cleaned up.

只要您的选择器确实在工作,那么检查数组长度的代码就没有问题。你想怎么做就怎么做。有很多方法可以清理代码,使其更简单、更可读。这是一个清理版,上面有我清理过的东西的注释。

var album_text = [];

$("input[name='album_text[]']").each(function() {
    var value = $(this).val();
    if (value) {
        album_text.push(value);
    }
});
if (album_text.length === 0) {
    $('#error_message').html("Error");
}

else {
  //send data
}

Some notes on what you were doing and what I changed.

一些关于你在做什么和我改变了什么的笔记。

  1. $(this) is always a valid jQuery object so there's no reason to ever check if ($(this)). It may not have any DOM objects inside it, but you can check that with $(this).length if you need to, but that is not necessary here because the .each() loop wouldn't run if there were no items so $(this) inside your .each() loop will always be something.
  2. $(this)始终是一个有效的jQuery对象,因此没有理由检查是否($(this)。它可能没有任何DOM对象,但是您可以使用$(this)检查它。如果需要的话,可以使用length,但是这里没有必要,因为如果没有项,.each()循环就不会运行,所以.each()循环中的$(this)将始终是一些东西。
  3. It's inefficient to use $(this) multiple times in the same function. Much better to get it once into a local variable and then use it from that local variable.
  4. 在同一个函数中多次使用$(this)是低效的。最好是将它一次放入局部变量,然后从该局部变量中使用它。
  5. It's recommended to initialize arrays with [] rather than new Array().
  6. 建议使用[]初始化数组,而不是使用new Array()。
  7. if (value) when value is expected to be a string will both protect from value == null, value == undefined and value == "" so you don't have to do if (value && (value != "")). You can just do: if (value) to check for all three empty conditions.
  8. 如果(value)当值被期望为一个字符串时,它将同时保护值== null, value ==未定义和值== ",所以你不必做if (value && (value != ""))。您可以这样做:if (value)检查所有三个空条件。
  9. if (album_text.length === 0) will tell you if the array is empty as long as it is a valid, initialized array (which it is here).
  10. 如果(album_text。length === = 0)将告诉您数组是否为空,只要它是一个有效的初始化数组(它在这里)。

What are you trying to do with this selector $("input[name='album_text[]']")?

您正在尝试使用这个选择器$(“input[name='album_text[]']]”)做什么?

#2


59  

User JQuery is EmptyObject to check whether array is contains elements or not.

用户JQuery是EmptyObject,用于检查数组是否包含元素。

var testArray=[1,2,3,4,5];
var testArray1=[];
console.log(jQuery.isEmptyObject(testArray)); //false
console.log(jQuery.isEmptyObject(testArray1)); //true

#3


6  

You should check for '' (empty string) before pushing into your array. Your array has elements that are empty strings. Then your album_text.length === 0 will work just fine.

在进入数组之前,应该检查“(空字符串)”。数组中的元素都是空字符串。那么你的album_text。长度=== 0就可以了。

#4


6  

I think it is dangerous to use $.isEmptyObject from jquery to check whether the array is empty, as @jesenko mentioned. I just met that problem.

我认为使用美元是危险的。isEmptyObject来自jquery,用于检查数组是否为空,如@jesenko所述。我刚遇到这个问题。

In the isEmptyObject doc, it mentions:

在isEmptyObject doc中,它提到:

The argument should always be a plain JavaScript Object

参数应该始终是一个普通的JavaScript对象

which you can determine by $.isPlainObject. The return of $.isPlainObject([]) is false.

可以通过$. isplainobject来确定。$. isplainobject([])返回值为false。