如果数组的所有值都为true,如何返回true,否则返回false? [英] How to return true if all values of array are true otherwise return false?
本文介绍了如果数组的所有值都为true,如何返回true,否则返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的数组:
I have a array like this:
var arr = [ true, true, true ];
现在我想得到 true
,因为上面数组的所有键都是 true
。
Now I want to get true
, because all keys of array above are true
.
另一个例子:
var arr = [ true, false, true ];
现在我需要获取 false
,因为数组中有一个 false
。
Now I need to get false
, because there is one false
in the array.
我该怎么做?
推荐答案
执行此操作的最短代码是 arr.every(x => x)
或 arr.every(function(x){return x})
以获得ES5兼容性。
The shortest code to do this would be arr.every(x => x)
or arr.every(function(x) {return x})
for ES5 compability.
每个
方法将返回true或false的函数对象作为参数,用于测试数组的每个元素。
The every
method takes as an argument a function object that returns either true or false, which is used to test each element of the array.
这篇关于如果数组的所有值都为true,如何返回true,否则返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文