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

后端 未结 15 1680
走了就别回头了
走了就别回头了 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:46

    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.

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

    This will give you all the keys from the object which is empty, undefined and null

    Object.keys(obj).filter((k)=> {
      if (obj[k] === "" || obj[k]===undefined || obj[k]===null) {
        return k;
      }
    });
    
    0 讨论(0)
  • 2020-12-08 04:52

    You can use Object.values() method to get all the object's values (as an array of object's values) and then check if this array of values contains null or "" values, with the help of _.includes method prvided by lodash library.

    const checkObjectProperties = obj => {
      const objValues = Object.keys(obj);
    
      if (_.includes(objValues, "") || _.includes(objValues, null)) {
        return false;
      } else {
        return true
      }
      
      const incorrectObjProps = { one: null, two: "", three: 78 }
      const correctObjProps = { one: "some string" }
      
      checkObjectProperties(incorrectObjProps) // return false
      checkObjectProperties(correctObjProps) // return true
    }

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

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

    Also if you are searching for only values are empty within the object,

    Object.values({ key: 0, key2: null, key3: undefined, key4: '' }).some(e => Boolean(e))
    // false
    
    Object.values({ key: 0, key2: null, key3: undefined, key4: "hello" }).some(e => Boolean(e))
    // true
    
    Object.values({ key: 1, key2: "hello" }).some(e => Boolean(e))
    // true
    
    0 讨论(0)
  • 2020-12-08 05:01

    You can use the Array.reduce prototype on your object's keys.

    Assuming that the object is structured as follows:

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

    you can use the following instruction to discover if all of it's properties are unset or set to empty string using just one line:

    Object.keys(obj).reduce((res, k) => res && !(!!obj[k] || obj[k] === false || !isNaN(parseInt(obj[k]))), true) // returns false
    

    If you want to discover if all of it's properties are set instead you have to remove the negation before the conditions and set the initial result value to true only if the object has keys:

    Object.keys(obj).reduce((res, k) => res && (!!obj[k] || obj[k] === false || !isNaN(parseInt(obj[k]))), Object.keys(obj).length > 0) // returns false as well
    
    0 讨论(0)
提交回复
热议问题