Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 679
Kay Avatar asked May 31 '18 08:05

Kay


People also ask

How do you check if all object keys has value?

To check if all values in object are equal to true : Use the Object. values() method to get an array of the object's values. Call the every() method on the array. The every method will test if all values in the array are equal to true and will return the result.

How do you check if an object is null in JS?

JavaScript uses the null value to represent a missing object. Use the strict equality operator ( === ) to check if a value is null . The typeof null returns 'object' , which is historical bug in JavaScript that may never be fixed.

How do you check if all object keys has false value?

To check if all of the values in an object are equal to false , use the Object. values() method to get an array of the object's values and call the every() method on the array, comparing each value to false and returning the result.


4 Answers

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()

like image 176
Eddie Avatar answered Oct 27 '22 13:10

Eddie


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.

like image 45
AndreCunha Avatar answered Oct 27 '22 11:10

AndreCunha


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.

like image 35
jovi De Croock Avatar answered Oct 27 '22 12:10

jovi De Croock


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!

like image 26
Ran Sasportas Avatar answered Oct 27 '22 11:10

Ran Sasportas