如果某个值存在于某个数组索引中,我如何检查JavaScript ?

时间:2021-01-09 07:18:32

Will this work for testing whether a value at position "index" exists or not, or is there a better way:

这是否适用于测试位置“索引”上的值是否存在,或者是否有更好的方法:

if(arrayName[index]==""){
     // do stuff
}

16 个解决方案

#1


705  

All arrays in JavaScript contain array.length elements, starting with array[0] up until array[array.length - 1]. By definition, an array element with index i is said to be part of the array if i is between 0 and array.length - 1 inclusive.

JavaScript中的所有数组都包含数组。长度元素,从数组[0]开始直到数组[数组]。长度- 1)。根据定义,如果i在0和数组之间,则具有索引i的数组元素被称为数组的一部分。长度- 1包容性。

That is, JavaScript arrays are linear, starting with zero and going to a maximum, and arrays don't have a mechanism for excluding certain values or ranges from the array. To find out if a value exists at a given position index (where index is 0 or a positive integer), you literally just use

也就是说,JavaScript数组是线性的,从0开始到最大值,并且数组没有排除某些值或数组范围的机制。要找出某个值是否存在于给定的位置索引(索引为0或正整数),您只需使用

if (index < array.length) {
  // do stuff
}

However, it is possible for some array values to be null, undefined, NaN, Infinity, 0, or a whole host of different values. For example, if you add array values by increasing the array.length property, any new values will be undefined.

但是,有些数组值可能是空的、未定义的、NaN、∞、0,或者是一个包含不同值的主机。例如,如果通过增加数组来添加数组值。长度属性,任何新值都是未定义的。

To determine if a given value is something meaningful, or has been defined. That is, not undefined, or null:

确定给定值是有意义的,还是已定义的。这是,不是未定义的,或null:

if (typeof array[index] !== 'undefined') {

or

if (typeof array[index] !== 'undefined' && array[index] !== null) {

Interestingly, because of JavaScript's comparison rules, my last example can be optimised down to:

有趣的是,由于JavaScript的比较规则,我最后一个例子可以优化为:

if (array[index] != null) {
  // The == and != operator consider null equal to only null or undefined

#2


349  

Can't we just do this:

我们不能这样做吗:

if(arrayName.length > 0){   
    //or **if(arrayName.length)**
    //this array is not empty 
}else{
   //this array is empty
}

#3


39  

Using only .length is not safe and will cause an error in some browsers. Here is a better solution:

仅使用.length不安全,在某些浏览器中会导致错误。这里有一个更好的解决方案:

if(array && array.length){   
   // not empty 
} else {
   // empty
}

or, we can use:

或者,我们可以用:

Object.keys(__array__).length

#4


20  

if(!arrayName[index]){
     // do stuff
}

#5


8  

if(arrayName.length > index && arrayName[index] !== null) {
    //arrayName[index] has a value
}

#6


6  

if(typeof arr ==='object' && arr instanceof Array ){
   if(!arr.length){
      println 'empty'
   }else{
      printn 'not Empty'
   }

}else{
   println 'Null'
}

If you mean by 'Null' -> Its elements are null or equals to '' , in this case : Check if the array is empty after filtering all 'null' elements

如果您的意思是'Null' ->,它的元素是Null或= ",在这种情况下:在过滤所有'Null'元素之后检查数组是否为空

if(!arr.clean().length){return 'is null'}

Of course ,Add Clean method before :

当然,在之前添加干净的方法:

Array.prototype.clean=function(){return this.filter(function(e){return (typeof  e !=='undefined')&&(e!= null)&&(e!='')})}

#7


6  

Short and universal approach

短的和通用的方法

If you want to check any array if it has falsy values (like false, undefined, null or empty strings) you can just use every() method like this:

如果要检查任何数组是否有falsy值(如false、undefined、null或空字符串),可以使用every()方法,如下所示:

array.every(function(element) {return !!element;}); // returns true or false

For example:

例如:

['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

If you need to get a first index of falsy value, you can do it like this:

如果需要得到第一个falsy值索引,可以这样做:

let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3

If you just need to check a falsy value of an array for a given index you can just do it like this:

如果你只需要检查给定索引的数组的falsy值,你可以这样做:

if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}

#8


5  

I would recommend creating a function like this:

我建议创建一个这样的函数:

function isEmptyEl(array, i) {
   return !(array[i]);
}

You could call it like this:

你可以这样称呼它:

if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}

Forcing the developer to adhere to the isEmptyEl interface will catch input errors such as an undefined arrayName or indexVal variables.

强迫开发人员坚持isEmptyEl接口将会捕获输入错误,如未定义的arrayName或indexVal变量。

(It's generally good practice to program defensively when programming in Javascript.)

(在使用Javascript编程时,最好进行防御性编程。)

You would get an error thrown like this if arrayName was not defined:

如果没有定义arrayName,则会抛出如下错误:

Uncaught ReferenceError: arrayName is not defined
    at <anonymous>:2:15
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

Similar results for an undefined indexVal.

未定义索引的类似结果。

You get an error if the array or index values do not exist.

如果数组或索引值不存在,则会出现错误。

For valid input, you'll only get a true if arrayName[indexVal] is any of the following:

对于有效的输入,只有当arrayName[indexVal]是以下任何一个时,您才会得到一个true:

  • null
  • undefined
  • 未定义的
  • NaN
  • empty string
  • 空字符串
  • 0
  • 0
  • false

#9


4  

It depends on what you mean with "empty".

这取决于你说的“空”是什么意思。

When you attempt to get the value of a property on an object which has no property with that name, you will get the value undefined.

当您试图获取一个对象上的属性值时,该对象没有具有该名称的属性,您将得到未定义的值。

That's what happens with sparse arrays: not all indices between 0 and array.length-1 exist.

这就是稀疏数组的情况:不是0和数组之间的所有索引。长度为1的存在。

So you could check if array[index] === undefined.

所以你可以检查数组[index] === undefined。

However, the property index could exist with an undefined value. If you want to filter out this case, you can use the in operator or hasOwnProperty, as described in How do I check if an object has a property in JavaScript?

但是,属性索引可以存在一个未定义的值。如果要过滤这种情况,可以使用in操作符或hasOwnProperty,如我如何检查一个对象在JavaScript中是否具有属性所描述的那样?

index in array;
array.hasOwnProperty(index);

If you want consider an existing property with an undefined or null value to not exist, you can use the loose comparison array[index] == undefined or array[index] == null.

如果您希望考虑一个现有的属性没有定义或空值,您可以使用宽松的比较数组[index] == = undefined或数组[index] = null。

If you know the array is not sparse, you could compare index with array.length. But to be safe, you may want to ensure that index really is an array index, see Check if property name is array index

如果知道数组不是稀疏的,可以将index与array.length进行比较。但是为了安全起见,您可能希望确保索引确实是一个数组索引,请查看属性名是否为数组索引

#10


0  

try this if array[index] is null

如果数组[index]为空,请尝试此操作

if (array[index] != null) 

#11


0  

With Lodash, you can do:

有了Lodash,你可以做到:

if(_.has(req,'documents')){
      if (req.documents.length)
      _.forEach(req.documents, function(document){
        records.push(document);
      });
} else {
}

if(_.has(req,'documents')) is to check whether our request object has a property named documents and if it has that prop, the next if (req.documents.length) is to validate if it is not an empty array, so the other stuffs like forEach can be proceeded.

如果(_.has(req,'documents')检查我们的请求对象是否有一个名为documents的属性,如果它有这个属性,那么下一个if(req.document .length)是验证它是否是一个空数组,因此可以继续执行其他类似forEach的内容。

#12


0  

To check if it has never been defined or if it was deleted:

检查它是否从未被定义或是否被删除:

if(typeof arrayName[index]==="undefined"){
     //the index is not in the array
}

also works with associative arrays and arrays where you deleted some index

还可以使用关联数组和数组来删除索引

To check if it was never been defined, was deleted OR is a null or logical empty value (NaN, empty string, false):

检查它是否从未被定义、被删除或是否为空或逻辑空值(NaN、空字符串、false):

if(typeof arrayName[index]==="undefined"||arrayName[index]){
     //the index is not defined or the value an empty value
}

#13


0  

I ran into this issue using laravel datatables. I was storing a JSON value called properties in an activity log and wanted to show a button based on this value being empty or not.

我在使用laravel数据时遇到了这个问题。我正在活动日志中存储一个名为properties的JSON值,并希望基于这个值是否为空显示一个按钮。

Well, datatables was interpreting this as an array if it was empty, and an object if it was not, therefore, the following solution worked for me:

datatables将它解释为一个数组如果它是空的,而对象不是空的,因此,下面的解决方案对我起作用:

render: function (data, type, full) {
    if (full.properties.length !== 0) {
        // do stuff
    }
}

An object does not have a length property.

对象没有长度属性。

#14


0  

OK, let's first see what would happens if an array value not exist in JavaScript, so if we have an array like below:

好的,让我们先看看如果JavaScript中不存在数组值会发生什么,如果我们有如下数组:

const arr = [1, 2, 3, 4, 5];

and now we check if 6 is there at index 5 or not:

现在我们来看看索引5是否有6

arr[5];

and we get undefined...

我们得到未定义……

So that's basically give us the answer, the best way to check if undefined, so something like this:

这就给出了答案,如果没有定义,最好的检验方法是这样的

if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}

It's better NOT doing this in this case:

在这种情况下最好不要这样做:

if(!arrayName[index]) {
  //Don't check like this..
}

Because imagine we have this array:

因为假设我们有这个数组

const arr = [0, 1, 2];

and we do:

我们做的是:

if(!arr[0]) {
  //This get passed, because in JavaScript 0 is falsy
}

So as you see, even 0 is there, it doesn't get recognised, there are few other things which can do the same and make you application buggy, so be careful, I list them all down:

就像你看到的,即使是0,它也不会被识别,几乎没有其他的东西可以做同样的事情,让你的应用程序陷入混乱,所以要小心,我把它们都列下来:

  1. undefined: if the value is not defined and it's undefined
  2. 未定义的:如果值未定义且未定义
  3. null: if it's null, for example if a DOM element not exists...
  4. null:如果它是null,例如DOM元素不存在……
  5. empty string: ''
  6. 空字符串:“
  7. 0: number zero
  8. 0:数字零
  9. NaN: not a number
  10. 南:不是一个数字
  11. false

#15


0  

I would like to point out something a few seem to have missed: namely it is possible to have an "empty" array position in the middle of your array. Consider the following:

我想指出一些似乎漏掉的东西:即在数组的中间有一个“空”数组位置是可能的。考虑以下:

let arr = [0, 1, 2, 3, 4, 5]

delete arr[3]

console.log(arr)      // [0, 1, 2, empty, 4, 5]

console.log(arr[3])   // undefined

The natural way to check would then be to see whether the array member is undefined, I am unsure if other ways exists

检查的自然方法是查看数组成员是否未定义,我不确定是否存在其他方法

if (arr[index] === undefined) {
  // member does not exist
}

#16


-1  

You can use Loadsh library to do this more efficiently, like:

您可以使用Loadsh库来更有效地实现这一点,比如:

if you have an array named "pets", for example:

如果您有一个名为“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) ) });

To check all array values for null or undefined values:

检查所有数组值是否为空值或未定义值:

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) ) });
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Check more examples in http://underscorejs.org/

在http://underscorejs.org/中查看更多的例子。

#1


705  

All arrays in JavaScript contain array.length elements, starting with array[0] up until array[array.length - 1]. By definition, an array element with index i is said to be part of the array if i is between 0 and array.length - 1 inclusive.

JavaScript中的所有数组都包含数组。长度元素,从数组[0]开始直到数组[数组]。长度- 1)。根据定义,如果i在0和数组之间,则具有索引i的数组元素被称为数组的一部分。长度- 1包容性。

That is, JavaScript arrays are linear, starting with zero and going to a maximum, and arrays don't have a mechanism for excluding certain values or ranges from the array. To find out if a value exists at a given position index (where index is 0 or a positive integer), you literally just use

也就是说,JavaScript数组是线性的,从0开始到最大值,并且数组没有排除某些值或数组范围的机制。要找出某个值是否存在于给定的位置索引(索引为0或正整数),您只需使用

if (index < array.length) {
  // do stuff
}

However, it is possible for some array values to be null, undefined, NaN, Infinity, 0, or a whole host of different values. For example, if you add array values by increasing the array.length property, any new values will be undefined.

但是,有些数组值可能是空的、未定义的、NaN、∞、0,或者是一个包含不同值的主机。例如,如果通过增加数组来添加数组值。长度属性,任何新值都是未定义的。

To determine if a given value is something meaningful, or has been defined. That is, not undefined, or null:

确定给定值是有意义的,还是已定义的。这是,不是未定义的,或null:

if (typeof array[index] !== 'undefined') {

or

if (typeof array[index] !== 'undefined' && array[index] !== null) {

Interestingly, because of JavaScript's comparison rules, my last example can be optimised down to:

有趣的是,由于JavaScript的比较规则,我最后一个例子可以优化为:

if (array[index] != null) {
  // The == and != operator consider null equal to only null or undefined

#2


349  

Can't we just do this:

我们不能这样做吗:

if(arrayName.length > 0){   
    //or **if(arrayName.length)**
    //this array is not empty 
}else{
   //this array is empty
}

#3


39  

Using only .length is not safe and will cause an error in some browsers. Here is a better solution:

仅使用.length不安全,在某些浏览器中会导致错误。这里有一个更好的解决方案:

if(array && array.length){   
   // not empty 
} else {
   // empty
}

or, we can use:

或者,我们可以用:

Object.keys(__array__).length

#4


20  

if(!arrayName[index]){
     // do stuff
}

#5


8  

if(arrayName.length > index && arrayName[index] !== null) {
    //arrayName[index] has a value
}

#6


6  

if(typeof arr ==='object' && arr instanceof Array ){
   if(!arr.length){
      println 'empty'
   }else{
      printn 'not Empty'
   }

}else{
   println 'Null'
}

If you mean by 'Null' -> Its elements are null or equals to '' , in this case : Check if the array is empty after filtering all 'null' elements

如果您的意思是'Null' ->,它的元素是Null或= ",在这种情况下:在过滤所有'Null'元素之后检查数组是否为空

if(!arr.clean().length){return 'is null'}

Of course ,Add Clean method before :

当然,在之前添加干净的方法:

Array.prototype.clean=function(){return this.filter(function(e){return (typeof  e !=='undefined')&&(e!= null)&&(e!='')})}

#7


6  

Short and universal approach

短的和通用的方法

If you want to check any array if it has falsy values (like false, undefined, null or empty strings) you can just use every() method like this:

如果要检查任何数组是否有falsy值(如false、undefined、null或空字符串),可以使用every()方法,如下所示:

array.every(function(element) {return !!element;}); // returns true or false

For example:

例如:

['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

If you need to get a first index of falsy value, you can do it like this:

如果需要得到第一个falsy值索引,可以这样做:

let falsyIndex; 

if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
  console.log(falsyIndex);
} // logs 3

If you just need to check a falsy value of an array for a given index you can just do it like this:

如果你只需要检查给定索引的数组的falsy值,你可以这样做:

if (!!array[index]) {
  // array[index] is a correct value
}
else {
  // array[index] is a falsy value
}

#8


5  

I would recommend creating a function like this:

我建议创建一个这样的函数:

function isEmptyEl(array, i) {
   return !(array[i]);
}

You could call it like this:

你可以这样称呼它:

if (isEmptyEl(arrayName, indexVal)) {
   console.log('arrayName[' + indexVal + '] is empty');
}

Forcing the developer to adhere to the isEmptyEl interface will catch input errors such as an undefined arrayName or indexVal variables.

强迫开发人员坚持isEmptyEl接口将会捕获输入错误,如未定义的arrayName或indexVal变量。

(It's generally good practice to program defensively when programming in Javascript.)

(在使用Javascript编程时,最好进行防御性编程。)

You would get an error thrown like this if arrayName was not defined:

如果没有定义arrayName,则会抛出如下错误:

Uncaught ReferenceError: arrayName is not defined
    at <anonymous>:2:15
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

Similar results for an undefined indexVal.

未定义索引的类似结果。

You get an error if the array or index values do not exist.

如果数组或索引值不存在,则会出现错误。

For valid input, you'll only get a true if arrayName[indexVal] is any of the following:

对于有效的输入,只有当arrayName[indexVal]是以下任何一个时,您才会得到一个true:

  • null
  • undefined
  • 未定义的
  • NaN
  • empty string
  • 空字符串
  • 0
  • 0
  • false

#9


4  

It depends on what you mean with "empty".

这取决于你说的“空”是什么意思。

When you attempt to get the value of a property on an object which has no property with that name, you will get the value undefined.

当您试图获取一个对象上的属性值时,该对象没有具有该名称的属性,您将得到未定义的值。

That's what happens with sparse arrays: not all indices between 0 and array.length-1 exist.

这就是稀疏数组的情况:不是0和数组之间的所有索引。长度为1的存在。

So you could check if array[index] === undefined.

所以你可以检查数组[index] === undefined。

However, the property index could exist with an undefined value. If you want to filter out this case, you can use the in operator or hasOwnProperty, as described in How do I check if an object has a property in JavaScript?

但是,属性索引可以存在一个未定义的值。如果要过滤这种情况,可以使用in操作符或hasOwnProperty,如我如何检查一个对象在JavaScript中是否具有属性所描述的那样?

index in array;
array.hasOwnProperty(index);

If you want consider an existing property with an undefined or null value to not exist, you can use the loose comparison array[index] == undefined or array[index] == null.

如果您希望考虑一个现有的属性没有定义或空值,您可以使用宽松的比较数组[index] == = undefined或数组[index] = null。

If you know the array is not sparse, you could compare index with array.length. But to be safe, you may want to ensure that index really is an array index, see Check if property name is array index

如果知道数组不是稀疏的,可以将index与array.length进行比较。但是为了安全起见,您可能希望确保索引确实是一个数组索引,请查看属性名是否为数组索引

#10


0  

try this if array[index] is null

如果数组[index]为空,请尝试此操作

if (array[index] != null) 

#11


0  

With Lodash, you can do:

有了Lodash,你可以做到:

if(_.has(req,'documents')){
      if (req.documents.length)
      _.forEach(req.documents, function(document){
        records.push(document);
      });
} else {
}

if(_.has(req,'documents')) is to check whether our request object has a property named documents and if it has that prop, the next if (req.documents.length) is to validate if it is not an empty array, so the other stuffs like forEach can be proceeded.

如果(_.has(req,'documents')检查我们的请求对象是否有一个名为documents的属性,如果它有这个属性,那么下一个if(req.document .length)是验证它是否是一个空数组,因此可以继续执行其他类似forEach的内容。

#12


0  

To check if it has never been defined or if it was deleted:

检查它是否从未被定义或是否被删除:

if(typeof arrayName[index]==="undefined"){
     //the index is not in the array
}

also works with associative arrays and arrays where you deleted some index

还可以使用关联数组和数组来删除索引

To check if it was never been defined, was deleted OR is a null or logical empty value (NaN, empty string, false):

检查它是否从未被定义、被删除或是否为空或逻辑空值(NaN、空字符串、false):

if(typeof arrayName[index]==="undefined"||arrayName[index]){
     //the index is not defined or the value an empty value
}

#13


0  

I ran into this issue using laravel datatables. I was storing a JSON value called properties in an activity log and wanted to show a button based on this value being empty or not.

我在使用laravel数据时遇到了这个问题。我正在活动日志中存储一个名为properties的JSON值,并希望基于这个值是否为空显示一个按钮。

Well, datatables was interpreting this as an array if it was empty, and an object if it was not, therefore, the following solution worked for me:

datatables将它解释为一个数组如果它是空的,而对象不是空的,因此,下面的解决方案对我起作用:

render: function (data, type, full) {
    if (full.properties.length !== 0) {
        // do stuff
    }
}

An object does not have a length property.

对象没有长度属性。

#14


0  

OK, let's first see what would happens if an array value not exist in JavaScript, so if we have an array like below:

好的,让我们先看看如果JavaScript中不存在数组值会发生什么,如果我们有如下数组:

const arr = [1, 2, 3, 4, 5];

and now we check if 6 is there at index 5 or not:

现在我们来看看索引5是否有6

arr[5];

and we get undefined...

我们得到未定义……

So that's basically give us the answer, the best way to check if undefined, so something like this:

这就给出了答案,如果没有定义,最好的检验方法是这样的

if("undefined" === typeof arrayName[index]) {
  //array value is not there...
}

It's better NOT doing this in this case:

在这种情况下最好不要这样做:

if(!arrayName[index]) {
  //Don't check like this..
}

Because imagine we have this array:

因为假设我们有这个数组

const arr = [0, 1, 2];

and we do:

我们做的是:

if(!arr[0]) {
  //This get passed, because in JavaScript 0 is falsy
}

So as you see, even 0 is there, it doesn't get recognised, there are few other things which can do the same and make you application buggy, so be careful, I list them all down:

就像你看到的,即使是0,它也不会被识别,几乎没有其他的东西可以做同样的事情,让你的应用程序陷入混乱,所以要小心,我把它们都列下来:

  1. undefined: if the value is not defined and it's undefined
  2. 未定义的:如果值未定义且未定义
  3. null: if it's null, for example if a DOM element not exists...
  4. null:如果它是null,例如DOM元素不存在……
  5. empty string: ''
  6. 空字符串:“
  7. 0: number zero
  8. 0:数字零
  9. NaN: not a number
  10. 南:不是一个数字
  11. false

#15


0  

I would like to point out something a few seem to have missed: namely it is possible to have an "empty" array position in the middle of your array. Consider the following:

我想指出一些似乎漏掉的东西:即在数组的中间有一个“空”数组位置是可能的。考虑以下:

let arr = [0, 1, 2, 3, 4, 5]

delete arr[3]

console.log(arr)      // [0, 1, 2, empty, 4, 5]

console.log(arr[3])   // undefined

The natural way to check would then be to see whether the array member is undefined, I am unsure if other ways exists

检查的自然方法是查看数组成员是否未定义,我不确定是否存在其他方法

if (arr[index] === undefined) {
  // member does not exist
}

#16


-1  

You can use Loadsh library to do this more efficiently, like:

您可以使用Loadsh库来更有效地实现这一点,比如:

if you have an array named "pets", for example:

如果您有一个名为“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) ) });

To check all array values for null or undefined values:

检查所有数组值是否为空值或未定义值:

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) ) });
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Check more examples in http://underscorejs.org/

在http://underscorejs.org/中查看更多的例子。