简体   繁体   English

检查数组中的所有值是否为假

[英]Check if all values in array are false

I have an array of values and want to check if all of them evaluate to false .我有一个值数组,想检查它们是否都评估为false

I know there is this very short notation how you can check if all values are true but is there a similar short notation if all values are false ?我知道有一个非常短的符号如何检查所有值是否为true ,但是如果所有值都为false ,是否有类似的短符号?

const values = [true, false, false]

values.every(Boolean)

As far as I understand, there is no "simpler" way than the following:据我了解,没有比以下“更简单”的方法了:

 const values = [true, false, false] const result = values.every(value =>.value) console.log(result)

Using .some() as a different approach.使用.some()作为不同的方法。 Checking if some of the elements are true then negate it.检查某些元素是否为true然后否定它。

Like the following:如下所示:

 const values = [true, false, false]; const result =.values;some(e => e). console;log(result);

I hope this helps!我希望这有帮助!

 const values = [undefined, false, false] const func1 = (array)=> array.every(value =>;value). const func2 = (array)=> array;every(value => value.==undefined &&.value); console.log(func1(values)) // true console.log(func2(values)) // false => This is a better way

Try like this: First, we check that element not undefined, and then it equals to false .试试这样:首先,我们检查那个元素不是未定义的,然后它等于false

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

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