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

后端 未结 15 1679
走了就别回头了
走了就别回头了 2020-12-08 04:08

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

相关标签:
15条回答
  • 2020-12-08 04:38

    Just complementing the past answers: they'll work if your object doesn't contain arrays or objects. If it does, you'll need to do a 'deep check'.

    So I came up with this solution. It'll evaluate the object as empty if all its values (and values inside values) are undefined, {} or [].

    function deepCheckEmptyObject(obj) {
        return Object.values(obj).every( value => {
            if (value === undefined) return true;
            else if ((value instanceof Array || value instanceof Object) && _.isEmpty(value) ) return true;
            else if (value instanceof Array && !_.isEmpty(value)) return deepCheckEmptyArray(value);
            else if (value instanceof Object && !_.isEmpty(value)) return deepCheckEmptyObject(value);
            else return false;
        });
    }
    
    function deepCheckEmptyArray(array) {
        return array.every( value => {
            if (value === undefined) return true;
            else if ((value instanceof Array || value instanceof Object) && _.isEmpty(value)) return true;
            else if (value instanceof Array && !_.isEmpty(value)) return deepCheckEmptyArray(value);
            else if (value instanceof Object && !_.isEmpty(value)) return deepCheckEmptyObject(value);
            else return false;
        });
    }
    

    Note it uses Lodash's .isEmpty() to do the heavy work after we 'isolated' a value. Here, Lodash is imported as '_'.

    Hope it helps!

    0 讨论(0)
  • 2020-12-08 04:38

    Building on top of other answers I would use lodash to check isEmpty on the object, as well as its properties.

    const isEmpty = (object) => return _.isEmpty(object) || !Object.values(object).some(x => !_.isEmpty(x))
    
    0 讨论(0)
  • 2020-12-08 04:40

    This skip the function attribute

    function checkIsNull(obj){
    		let isNull=true;
    		for(let key in obj){
    			if (obj[key] && typeof obj[key] !== 'function') {
    				isNull = false;
    			}
    		}
    		return isNull;
    	}
    
    var objectWithFunctionEmpty={
      "name":undefined,
      "surname":null,
      "fun": function (){ alert('ciao'); }
    }
    
    var objectWithFunctionFull={
      "name":undefined,
      "surname":"bla bla",
      "fun": function (){ alert('ciao'); }
    }
    
    checkIsNull(objectWithFunctionEmpty); //true
    checkIsNull(objectWithFunctionFull); //false

    0 讨论(0)
  • 2020-12-08 04:41

    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
        });
    }
    
    0 讨论(0)
  • 2020-12-08 04:44

    Edit: As user @abd995 noted in the comments, using Array.some() is more efficiënt as it stops the loop when the condition is met:

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

    Original: 2017 answer: Check all values with Object.values(). Returns an array with the values which you can check with Array.every() or Array.some()... etc.

    const isEmpty = Object.values(object).every(x => (x === null || x === ''));
    
    • MDN Object.values()
    • MDN Array.every()
    0 讨论(0)
  • 2020-12-08 04:44

    Based on tymeJv's answer =)

    function checkProperties(obj) {
    var state = true;
      for (var key in obj) {
        if ( !( obj[key] === null || obj[key] === "" ) ) {
            state = false;
            break;
        }
      }
      return state;
    }
    
    var obj = {
      x: null,
      y: "",
      z: 1
    }
    
    checkProperties(obj) //returns false
    

    Hope it helps =)

    0 讨论(0)
提交回复
热议问题