最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Check if Javascript Object only contains falsy values - Stack Overflow

programmeradmin2浏览0评论

I got an object like this :

var obj1 = {foo: false, bar: ''};
var obj2 = {foo: false, bar: '1'};
var obj3 = {foo: true,  bar: ''};
var obj4 = {foo: true,  bar: '1'};

I want a simple function to check those objects if all of their values are false or not. In this given example only obj1 should trigger a false - cause all of their values are false. obj2, obj3 and obj4 own at least one value which is true.

Is there a simple solution to do this?

I got an object like this :

var obj1 = {foo: false, bar: ''};
var obj2 = {foo: false, bar: '1'};
var obj3 = {foo: true,  bar: ''};
var obj4 = {foo: true,  bar: '1'};

I want a simple function to check those objects if all of their values are false or not. In this given example only obj1 should trigger a false - cause all of their values are false. obj2, obj3 and obj4 own at least one value which is true.

Is there a simple solution to do this?

Share Improve this question edited Oct 22, 2019 at 9:49 B--rian 5,88011 gold badges47 silver badges100 bronze badges asked Jan 14, 2016 at 11:25 KrisKris 5753 gold badges11 silver badges26 bronze badges
Add a comment  | 

9 Answers 9

Reset to default 7

As a one-liner:

!Object.keys(obj1).some(function(k) {return obj1[k];});
// Array.some returns true if any callback returns true
// You want true if all return false, aka if none return true
// So just negate Array.some

As a more readable method:

var ok = true, k;
for( k in obj1) if( obj1.hasOwnProperty(k)) {
    if( obj1[k]) {
        ok = false;
        break;
    }
}

You can use the some function:

[ob1, ob2, ob3, obj4].some(function(obj) { return obj.foo })

For each object: if every property value is false return false, otherwise true:

var out = [obj1, obj2, obj3, obj4].map(function (obj) {
  return !Object.keys(obj).every(function (p) {
    return !obj[p];
  })
}); // [false, true, true, true]

DEMO

There is a flaw with the other implementations of .some() you want to return values where all properties are a falsy value, not just where any property is a falsy value as per the OP requirements.

So this will do it

function isFalse(obj) {
  return Object
    .keys(obj)
    .every(function (k) {
      return !obj[k];
    });
}

See jsFiddle for proof https://jsfiddle.net/0se3yh62/

The other benefit of Object.keys it will only return own properties so you don't have to check in a manual loop.

const isObjectFilled = obj => Object.values(obj).filter(value => !!value).length > 0

isObjectFilled(obj1) // false
isObjectFilled(obj2) // true
isObjectFilled(obj3) // true
isObjectFilled(obj4) // true

"The Object.values() method returns an array of a given object's own enumerable property values" (MDN)

Once i've this array of values, i'm going to filter it by removing each falsy values.

.filter(value => !!value) will filter the array by returning only truthy values.

If the length of the filtered array is greater than 0, it means that at least one of the object's values is truthy. Then the function will return true

If the filtered array is empty, it means that all of the object's values are falsy. Then the function will return false

I assume you want a dynamic function that would work on any object, so how about just iterating all of the object's indexes?

function objIsFalsy(obj) {
    for(var i in obj) {
        if(obj[i]) return false;
    }
    return true;
}

Follow these steps :-

  1. Put all your object variables in an array.
  2. Make a foreach loop for the above array.
  3. Inside the loop check for object key's value.

Loop on your object:

function isFalsyObj(obj) {
  for(var elt in obj) if (obj[elt]!=false) return false;
  return true;
}
var obj1 = {foo: false, bar: ''};
var obj2 = {foo: false, bar: '1'};
var obj3 = {foo: true,  bar: ''};
var obj4 = {foo: true,  bar: '1'};
console.log(isFalsyObj(obj1));//true
console.log(isFalsyObj(obj2));//false
console.log(isFalsyObj(obj3));//false
console.log(isFalsyObj(obj4));//false

Check this

isvalidObj(obj1); // false

isvalidObj(obj2); // true

function isvalidObj(obj){
    for(var i in obj){ 
      if(obj[i]) return true;
    }
    return false;
}
发布评论

评论列表(0)

  1. 暂无评论