简体   繁体   English

如何在JavaScript中查找特定的数组变量是否为空?

[英]How to find whether the particular array variable is empty or not in JavaScript?

I have an array variable like Array[0] , and now I need to check whether that Array[0] contains value or not inside of it. 我有一个像Array[0]这样的数组变量,现在我需要检查Array[0]是否在其中包含值。 What should I do? 我该怎么办?

If you have an array and call it eg. 如果您有一个数组,并调用它例如。 list , then you can check if it has some elements in the following manner: list ,然后可以通过以下方式检查它是否包含某些元素:

if (list.length){
    // has some elements (exactly list.length elements)
} else {
    // does not have any elements
}

See the following for details: Documentation on length property of Array objects in JavaScript 有关详细信息,请参见以下内容: JavaScript中Array对象的length属性的文档

Checks if that array exists and does something if not so. 检查该数组是否存在,如果不存在,请执行某些操作。

if(!array[0])
{
  //do something
}
if(array[0] !== undefined){
 // array[0] is defined.
}
//pass array variable to this function

function isEmpty(array){
   if(array.length == 0)
    return true;
   else
    return false; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM