how to check if all object keys has false values

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)