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

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


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 !== '');


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


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    });}