如何检查数组是否为空或不存在? [英] How to check if array is empty or does not exist?

查看:45
本文介绍了如何检查数组是否为空或不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查数组是否为空或不存在的最佳方法是什么?

What's the best way to check if an array is empty or does not exist?

类似的东西?

if(array.length < 1 || array == undefined){
    //empty
}

推荐答案

您想先检查 undefined.如果反过来,如果数组未定义,则会产生错误.

You want to do the check for undefined first. If you do it the other way round, it will generate an error if the array is undefined.

if (array === undefined || array.length == 0) {
    // array empty or does not exist
}

更新

这个答案得到了相当多的关注,所以我想指出,我最初的答案最重要的是解决了问题中被评估条件的错误顺序.从这个意义上说,它无法解决多种场景,例如 null 值、具有 length 属性的其他类型的对象等.它也不是很惯用的 JavaScript.

Update

This answer is getting a fair amount of attention, so I'd like to point out that my original answer, more than anything else, addressed the wrong order of the conditions being evaluated in the question. In this sense, it fails to address several scenarios, such as null values, other types of objects with a length property, etc. It is also not very idiomatic JavaScript.

万无一失的方法
从评论中得到一些启发,下面是我目前认为的检查数组是否为空或不存在的万无一失的方法.它还考虑到变量可能不是指数组,而是指具有 length 属性的其他类型的对象.

The foolproof approach
Taking some inspiration from the comments, below is what I currently consider to be the foolproof way to check whether an array is empty or does not exist. It also takes into account that the variable might not refer to an array, but to some other type of object with a length property.

if (!Array.isArray(array) || !array.length) {
  // array does not exist, is not an array, or is empty
  // ⇒ do not attempt to process array
}

分解:

  1. Array.isArray(),不出所料,检查它的参数是否是一个数组.这会清除诸如 nullundefined 和任何其他不是数组的值.
    请注意,这也将消除类似数组的对象,例如 参数 对象和 DOM NodeList 对象.根据您的情况,这可能不是您所追求的行为.

  1. Array.isArray(), unsurprisingly, checks whether its argument is an array. This weeds out values like null, undefined and anything else that is not an array.
    Note that this will also eliminate array-like objects, such as the arguments object and DOM NodeList objects. Depending on your situation, this might not be the behavior you're after.

array.length 条件检查变量的 length 属性是否计算为 真实 值.因为前面的条件已经确定我们确实在处理一个数组,所以不需要更严格的比较,比如 array.length != 0array.length !== 0在这里.

The array.length condition checks whether the variable's length property evaluates to a truthy value. Because the previous condition already established that we are indeed dealing with an array, more strict comparisons like array.length != 0 or array.length !== 0 are not required here.

务实的方法
在很多情况下,以上可能看起来有点矫枉过正.也许您正在使用像 TypeScript 这样的高阶语言,它在编译时为您完成大部分类型检查,或者您真的不关心对象实际上是一个数组,还是只是类似数组.

The pragmatic approach
In a lot of cases, the above might seem like overkill. Maybe you're using a higher order language like TypeScript that does most of the type-checking for you at compile-time, or you really don't care whether the object is actually an array, or just array-like.

在这些情况下,我倾向于使用以下更惯用的 JavaScript:

In those cases, I tend to go for the following, more idiomatic JavaScript:

if (!array || !array.length) {
    // array or array.length are falsy
    // ⇒ do not attempt to process array
}

或者,更常见的是,它的倒数:

Or, more frequently, its inverse:

if (array && array.length) {
    // array and array.length are truthy
    // ⇒ probably OK to process array
}

随着可选链操作符的引入(Elvis 运算符)在 ECMAScript 2020 中,这可以进一步缩短:

With the introduction of the optional chaining operator (Elvis operator) in ECMAScript 2020, this can be shortened even further:

if (!array?.length) {
    // array or array.length are falsy
    // ⇒ do not attempt to process array
}

或者相反:

if (array?.length) {
    // array and array.length are truthy
    // ⇒ probably OK to process array
}

这篇关于如何检查数组是否为空或不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆