how to check if all object keys has false values how to check if all object keys has false values arrays arrays

how to check if all object keys has false values


This is a very simple solution that requires JavaScript 1.8.5.

Object.keys(obj).every((k) => !obj[k])

Examples:

obj = {'a': true, 'b': true}Object.keys(obj).every((k) => !obj[k]) // returns falseobj = {'a': false, 'b': true}Object.keys(obj).every((k) => !obj[k]) // returns falseobj = {'a': false, 'b': false}Object.keys(obj).every((k) => !obj[k]) // returns true

Alternatively you could write

Object.keys(obj).every((k) => obj[k] == false)Object.keys(obj).every((k) => obj[k] === false)  // or thisObject.keys(obj).every((k) => obj[k])  // or this to return true if all values are true

See the Mozilla Developer Network Object.keys()'s reference for further information.


This will do the trick...

var result = true;for (var i in saver) {    if (saver[i] === true) {        result = false;        break;    }}

You can iterate objects using a loop, either by index or key (as above).

If you're after tidy code, and not repeating that then simply put it in a function...

Object.prototype.allFalse = function() {     for (var i in this) {        if (this[i] === true) return false;    }    return true;}

Then you can call it whenever you need, like this...

alert(saver.allFalse());

Here's a working sample...