如果数组的所有值都为真,如何返回真,否则返回假? [英] How to return true if all values of array are true otherwise return false?

查看:125
本文介绍了如果数组的所有值都为真,如何返回真,否则返回假?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数组:

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.

every 方法将返回 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.

这篇关于如果数组的所有值都为真,如何返回真,否则返回假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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