how to check if all object keys has false values

63,115

Solution 1

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...

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

var saver = {
        title: false,
        preview: false,
        body: false,
        bottom: false,
        locale: false
};

console.log("all are false - should alert 'true'");
console.log(saver.allFalse());

saver.body = true;

console.log("one is now true - should alert 'false'");
console.log(saver.allFalse());

Solution 2

Updated version. Thanks @BOB for pointing out that you can use values directly:

Object.values(obj).every((v) => v === false)

Also, the question asked for comparison to false and most answers below return true if the object values are falsy (eg. 0, undefined, null, false), not only if they are strictly false.


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 false

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

obj = {'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 this
Object.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.

Solution 3

In a comment you ask if you can avoid iteration. You can if you use a javascript library supporting a functional approach, like Underscore, Lodash or Sugar.

With Underscore and Lodash you can write something like this:

var result = _.every(_.values(saver), function(v) {return !v;});

With Sugar you can simply write:

var result = Object.all(saver,false);

Solution 4

Use array.some()

It's more clean and understandable! And it can save us running time, because once the function condition exist once, it goes out of the loop and returns true.

Object.values(obj).some(val => val)

if you actually need strict equality to false write this:

Object.values(obj).some(val => val !== false)

Object.values(obj) make an array with the values of each key.

Solution 5

Short and handy one-liner, fully supported by browsers:

Object.keys(saver).every(k => saver[k] === false);

or

Object.values(saver).every(v => v === false);

(careful tho, Object.values() is not supported by IE yet.)

Share:
63,115
Andrey Kryukov
Author by

Andrey Kryukov

Updated on November 07, 2021

Comments

  • Andrey Kryukov
    Andrey Kryukov over 2 years

    JS Object:

    var saver = {
            title: false,
            preview: false,
            body: false,
            bottom: false,
            locale: false
    };
    

    The question is how to check if all values is false?

    I can use $.each() jQuery function and some flag variable, but there may be a better solution?