admin管理员组

文章数量:1135123

In JavaScript, I need to know if all object items are set to 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.

In JavaScript, I need to know if all object items are set to 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.

Share Improve this question edited Jun 20, 2017 at 16:26 Candlejack 44510 silver badges23 bronze badges asked Jun 14, 2013 at 21:44 napaliasnapalias 1,2051 gold badge11 silver badges21 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 112

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

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

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

Object.values(yourTestObject).every(Boolean)

Or with stricter true checks

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

In modern browsers:

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

If you really want to check for true rather than just a truthy value:

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

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 true if all values are true-ish, not necessarily exactly equal to the Boolean value of True. Also, it will scan all properties 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.

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.

You can use every from lodash

const obj1 = { a: 1, b: 2, c: true };
const obj2 = { a: true, b: true, c: true }; 

_.every(obj1, true);  // false
_.every(obj2, true);  // true

本文标签: How to know if all javascript object values are trueStack Overflow