如何知道所有 javascript 对象值是否为真?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17117712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:08:33  来源:igfitidea点击:

How to know if all javascript object values are true?

javascript

提问by napalias

In JavaScript, I need to know if all object items are set to true.

在 JavaScript 中,我需要知道是否所有对象项都设置为 true。

If I have the following object:

如果我有以下对象:

var myObj = {title:true, name:true, email:false};

I could write something like this :

我可以写这样的东西:

 if(myObj.title && myObj.name && myObj.email){
 /*Some code */
};

But I am looking for the simplest way to write it. eg :

但我正在寻找最简单的写法。例如:

if(myObj all is true){
/*Some code */
};

I might have another object with 10-20 items inside it, and will need to know if all are true.

我可能有另一个对象,里面有 10-20 个项目,并且需要知道所有内容是否正确。

采纳答案by Mike Christensen

How about something like:

怎么样:

    function allTrue(obj)
    {
      for(var o in obj)
          if(!obj[o]) return false;
        
      return true;
    }
    
    var myObj1 = {title:true, name:true, email:false};
    var myObj2 = {title:true, name:true, email:true};

    document.write('<br />myObj1 all true: ' + allTrue(myObj1));
    document.write('<br />myObj2 all true: ' + allTrue(myObj2));

    

A few disclaimers:This will return trueif all values are true-ish, not necessarily exactly equal to the Boolean value of True. Also, it will scan allproperties of the passed in object, including its prototype. This may or may not be what you need, however it should work fine on a simple object literal like the one you provided.

一些免责声明:true如果所有值都是true-ish,则这将返回,不一定完全等于 True 的布尔值。此外,它将扫描传入对象的所有属性,包括其原型。这可能是也可能不是您需要的,但是它应该可以在像您提供的那样的简单对象文字上正常工作。

回答by elclanrs

In modern browsers:

在现代浏览器中:

var allTrue = Object.keys(myObj).every(function(k){ return myObj[k] });

A shorter alternative to this is:

一个更短的替代方案是:

var allTrue = myObj.every(function(i) { return i; });

If you reallywant to check for truerather than just a truthy value:

如果你真的想检查true而不仅仅是一个真实的值:

var allTrue = Object.keys(myObj).every(function(k){ return myObj[k] === true });

回答by Nachiketha

With ES2017 Object.values()life's even simpler.

使用 ES2017 Object.values()生活更简单。

Object.values(yourTestObject).every(item => item)

Even shorter version with Boolean()function [thanks to xab]

使用Boolean()函数的更短版本[感谢xab]

Object.values(yourTestObject).every(Boolean)

Or with stricter truechecks

或者更严格的真实检查

Object.values(yourTestObject)
    .every(item => item === true)

回答by Jordan Ramstad

Quickest way is a loop

最快的方法是循环

for(var index in myObj){
  if(!myObj[index]){ //check if it is truly false
    var fail = true
  }
}
if(fail){
  //test failed
}

This will loop all values in the array then check if the value is false and if it is then it will set the fail variable, witch will tell you that the test failed.

这将循环数组中的所有值,然后检查该值是否为假,如果为假,则将设置失败变量,巫婆会告诉您测试失败。