检查数组是否为空或不存在。 JS [重复]

本文翻译自:Check if array is empty or does not exist. JS [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

What's the best way to check if an array is empty or doesn't exist? 检查数组是空还是不存在的最佳方法是什么?

Something like this? 像这样的东西?

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

#1楼

参考:https://stackoom.com/question/1EowA/检查数组是否为空或不存在-JS-重复


#2楼

You want to do the check for undefined first. 您想先检查undefined 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
}

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. 从这个意义上说,它无法解决几个场景,例如null值,具有length属性的其他类型的对象等。它也不是非常惯用的JavaScript。

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. 它还考虑到变量可能不引用数组,而是引用具有length属性的其他类型的对象。

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

To break it down: 要打破它:

  1. Array.isArray() , unsurprisingly, checks whether its argument is an array. Array.isArray()Array.isArray()检查它的参数是否是一个数组。 This weeds out values like null , undefined and anything else that is not an array. 这会清除像nullundefined和其他任何不是数组的值。
    Note that this will also eliminate array-like objects , such as the arguments object and DOM NodeList objects. 请注意,这也将消除类似数组的对象 ,例如arguments对象和DOM NodeList对象。 Depending on your situation, this might not be the behavior you're after. 根据您的情况,这可能不是您所追求的行为。

  2. The array.length condition checks whether the variable's length property evaluates to a truthy value. array.length条件检查变量的length属性是否计算为truthy值。 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. 因为先前的条件已经确定我们确实在处理数组,所以这里不需要更严格的比较,例如array.length != 0array.length !== 0

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. 也许您正在使用像TypeScript这样的高阶语言,它在编译时为您执行大部分类型检查,或者您实际上并不关心对象实际上是数组还是数组类。

In those cases, I tend to go for the following, more idiomatic JavaScript: 在这些情况下,我倾向于使用以下更惯用的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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值