Kyle.Belanger Kyle.Belanger - 19 days ago 7
Javascript Question

Check if any key values are false in an object

I'm looking for a simple solution to check if any key values are

false
in an object.

I have an object with several unique keys, however, they only contain boolean values (
true
or
false
)

var ob = { stack: true,
overflow: true,
website: true
};


I know that I can get the number of keys in an Object, with the following line:

Object.keys(ob).length // returns 3


Is there a built in method to check if any key value is false without having to loop through each key in the object?

Answer

You can use the Array.some method:

var hasFalseKeys = Object.keys(ob).some(k => !ob[k]);
Comments