Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if all attributes on a javascript object are null or an empty string

What is the most elegant way to determine if all attributes in a javascript object are either null or the empty string? It should work for an arbitrary number of attributes.

{'a':null, 'b':''} //should return true for this object
{'a':1, 'b':''} //should return false for this object
{'a':0, 'b':1} //should return false
{'a':'', 'b':''} //should return true
like image 917
J-bob Avatar asked Dec 30 '14 17:12

J-bob


People also ask

How do you check if JavaScript object is null?

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 keys of an object are empty?

keys() is a static method that returns an Array when we pass an object to it, which contains the property names (keys) belonging to that object. We can check whether the length of this array is 0 or higher - denoting whether any keys are present or not. If no keys are present, the object is empty: Object.

How do you check if all object keys has value?

To check if all of the values in an object are equal, use the Object. values() method to get an array of the object's values and convert the array to a Set . If the Set contains a single element, then all of the values in the object are equal. Copied!

How check variable is empty or not in JavaScript?

Say, if a string is empty var name = "" then console. log(! name) returns true . this function will return true if val is empty, null, undefined, false, the number 0 or NaN.


5 Answers

Check all values with Object.values. It returns an array with the values, which you can check with Array.prototype.every or Array.prototype.some:

const isEmpty = Object.values(object).every(x => x === null || x === '');
const isEmpty = !Object.values(object).some(x => x !== null && x !== '');
like image 151
PVermeer Avatar answered Oct 21 '22 07:10

PVermeer


Create a function to loop and check:

function checkProperties(obj) {
    for (var key in obj) {
        if (obj[key] !== null && obj[key] != "")
            return false;
    }
    return true;
}

var obj = {
    x: null,
    y: "",
    z: 1
}

checkProperties(obj) //returns false
like image 42
tymeJV Avatar answered Oct 21 '22 07:10

tymeJV


Here's my version, specifically checking for null and empty strings (would be easier to just check for falsy)

function isEmptyObject(o) {
    return Object.keys(o).every(function(x) {
        return o[x]===''||o[x]===null;  // or just "return o[x];" for falsy values
    });
}
like image 30
adeneo Avatar answered Oct 21 '22 06:10

adeneo


let obj = { x: null, y: "hello", z: 1 };
let obj1 = { x: null, y: "", z: 0 };

!Object.values(obj).some(v => v);
// false

!Object.values(obj1).some(v => v);
// true
like image 13
Gaurav Arya Avatar answered Oct 21 '22 07:10

Gaurav Arya


Using Array.some() and check if the values are not null and not empty is more efficient than using Array.every and check it the other way around.

const isEmpty = !Object.values(object).some(x => (x !== null && x !== ''));

This answer should just make the excellent comment of user abd995 more visible.

like image 8
deamon Avatar answered Oct 21 '22 05:10

deamon