2

I have an object that looks like this:

objectProp = {
  property1: [],
  property2: [],
}

I want to check if all properties(that are string arrays) if this object are null and return true. I am for some reason stuck. How can I do this? I am using "target": "es2015"

I tried this but does not work:

 if (!Object.keys(this.objectProp).every(array => array.length > 0)) {
      console.log('all are empety');
    }
1

4 Answers 4

6

You need to use Object.values(this.objectProp) instead of Object.keys(this.objectProp) for this to work. As Object.keys method returns an array of a given object's own enumerable property names and Object.values method returns an array of a given object's own enumerable property values, which is actually what you are looking for.

let objectProp = {
  property1: [],
  property2: [],
}

console.log(Object.keys(objectProp))

console.log(Object.values(objectProp))

For Es2015, you can try this:

let obj = {
  property1: [],
  property2: [],
}

// Log is print only when all arrays are empty
if (Object.keys(obj).map(e => obj[e]).every(a => a.length === 0)) {
  console.log('all are empty');
}

Sign up to request clarification or add additional context in comments.

3 Comments

it would be a nice solution but i am using "target": "es2015"
You can use the simple polyfill then mentioned here
@KathrineHanson Instead of adding that to every answer after the answer has been written, please edit your question and add it there...
2

By using Object.keys() you can do it as following

var objectProp = {
  property1: [],
  property2: [],
}

if(Object.keys(objectProp).every(key => objectProp[key] && objectProp[key].length === 0)){
  console.log('All is empty')
}

6 Comments

it would be a nice solution but i am using "target": "es2015"
Updated as you requested
Slight logic bug, but the OP did the same !Object.value & >0 with every, means if a single one is not empty, it will print All is empty,.. It should be Object.values & <=0 with every. Or alternatively ! and some.
I think you did not understand what i want. I want to check if ALL properties of the object are empty. Not just if only one is empty but ALL.
@Keith u are right that it does not work but the way you wrote it I do not understand. can u write it completely? thanks.
|
2

You should use Object.values as follow:

if (!Object.values(this.objectProp).every(({length}) => Boolean(length))) {
  console.log('all are empety');
}

1 Comment

it would be a nice solution but i am using "target": "es2015"
0

Most answers seem to have missed the part about your logic been slightly wrong. They will say empty, if only single item is empty,. This is because the logic is !every > 0 But switching this logic to every <= 0 will get what you want.

eg.

The below example will return true, true for the broken version. But true, false for the fixed one.

var objectProp = {
  property1: [],
  property2: [],
};

var objectProp2 = {
  property1: [],
  property2: [],
  property3: ['not empty']
};


function allIsEmptyBroken(objectProp) {
  return (!Object.keys(objectProp).every(key => objectProp[key] && objectProp[key].length > 0));
}

function allIsEmptyFixed(objectProp) {
  return (Object.keys(objectProp).every(key => objectProp[key] && objectProp[key].length <= 0));
}


console.log('allIsEmptyBroken');
console.log(allIsEmptyBroken(objectProp));
console.log(allIsEmptyBroken(objectProp2));

console.log('allIsEmptyFixed');
console.log(allIsEmptyFixed(objectProp));
console.log(allIsEmptyFixed(objectProp2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.