Kay
Kay

Reputation: 19660

How to check if every properties in an object are null

I have an object, sometimes it is empty like so {} other times it will have properties that are set to null.

{
  property1: null,
  property2: null
}

How can I determine if ALL the properties within this object is null? If they are all null then return false.

At the moment I'm using lodash to check for the first case where the object is simply {} empty. But I also need to cover the second case.

if (isEmpty(this.report.device)) {
  return false;
}
return true;

Upvotes: 17

Views: 49197

Answers (7)

Hassam usmani
Hassam usmani

Reputation: 1

For Es2015

if (Object.keys(obj).map(e => obj[e]).every(a => a.length === 0)) {
  console.log('all are empty');
}

Upvotes: 0

Vikramaditya
Vikramaditya

Reputation: 5574

In order to check if object has null value for all the keys. One cleaner approach could be.

const set = new Set(Object.values(obj));
const hasOnlyNullValues = set.size === 1 && set.has(null);

Upvotes: 0

AndreCunha
AndreCunha

Reputation: 183

Approach using .some() instead of .every():

function isEmpty (obj) {
    return !Object.values(obj).some(element => element !== null);
}

This function (named isEmpty to match the name given in the question) shall return false if any obj property is not null and true otherwise.

Upvotes: 7

Eddie
Eddie

Reputation: 26844

You can use Object.values to convert the object into array and use every to check each element. Use ! to negate the value.

let report = {
  property1: null,
  property2: null,
}

let result = !Object.values(report).every(o => o === null);

console.log(result);

An example some elements are not null

let report = {
  property1: null,
  property2: 1,
}

let result = !Object.values(report).every(o => o === null);

console.log(result);

Doc: Object.values(), every()

Upvotes: 38

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

Use Object.entries and Array.every

let obj = {
   property1: null,
   property2: null,
};

function isEmpty(o) {
  return Object.entries(o).every(([k,v]) => v === null);
}

if(isEmpty(obj)) {
   console.log("Object is empty");
} 

Upvotes: 0

Ran Sasportas
Ran Sasportas

Reputation: 2266

This is very simple and can be done with a one liner !

function IsAllPropertiesNull(obj) {
 return Object.values(obj).every(v=>v == null);
}

a = {'a': null, 'b':null};
var isAllNull = IsAllPropertiesNull(a) 
// isAllNull = true

explanation - get all values of object - iterate them and check for null

Good luck!

Upvotes: 1

jovi De Croock
jovi De Croock

Reputation: 615

You can use the Object.keys() method this will return all keys in that Object as an Array. This makes it possible to do Object.keys(this.report.device).filter(key => !this.report.device[key] === null), which will return you the amount of not null keys, if this is 0 then you have your answer.

In essence relying on null properties is not such a good approach it's better to make those properties undefined or just to return a flat Object from your API.

Hope this helped.

Upvotes: 4

Related Questions