Check if any key values are false in an object

You can use the Array.some method:

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

You can create an arrow function isAnyKeyValueFalse, to reuse it in your application, using Object.keys() and Array.prototype.find().

Code:

const ob = {
  stack: true,
  overflow: true,
  website: true
};
const isAnyKeyValueFalse = o => !!Object.keys(o).find(k => !o[k]);

console.log(isAnyKeyValueFalse(ob));

Here's how I would do that:

Object.values(ob).includes(false);      // ECMAScript 7

// OR

Object.values(ob).indexOf(false) >= 0;  // Before ECMAScript 7

Tags:

Javascript