Home > database >  check is an object is fully empty
check is an object is fully empty

Time:09-16

this object has properties, but no values, or at least empty arrays. I would like to do a condition checking if the object test has no content in the array then return true.

const test = {
  "106596": [],
  "107014": []
};

const output = Object.entries(clientData).every(element => Object.values(element).some(val =>  val === "");

console.log(output);

CodePudding user response:

Just change the code inside some(). Check arrays length

const output = Object.entries(clientData).every(element => Object.values(element).some(val => Array.isArray(element) && val.length == 0);

const test = {
    "106596": [],
    "107014": []
     };


Output : false

When added an element inside test's element's array ;

const test = {
     "106596": [1],
     "107014": []
    };


Output : true

CodePudding user response:

There are – as you can see – many ways to implement this functionality, my own approach is below, with explanatory comments in the code:

const test = {
  "106596": [],
  "107014": []
},
// we declare a function to check if the array-values are empty,
// we pass in the Object we're testing, and from we use
// Object.values() to retrieve the values of the Object; we
// then iterate over the array-values using Array.prototype.every():
arrayValuesEmpty = (obj) => Object.values(obj).every(
  // along with an anoymous Arrow function to check if the
  // current array-element value ('val') is an Array (using
  // Array.isArray() and that its length property is exactly zero:
    (val) => Array.isArray(val) && val.length === 0
);

console.log(arrayValuesEmpty(test));

The above does have some naive assumptions, that all the Object values are Arrays and assumes that the passed-in argument will be an Object with properties; to guard against those assumptions:

const test = {
    // adding new properties, String, Number and Boolean:
    stringProperty: 'Steven',
    numericalProperty: 3,
    booleanProperty: false,
    "106596": [],
    "107014": []
  },
  // here we pass in a default value (empty Object) to the function if
  // the user supplies no argument:
  arrayValuesEmpty = (obj = {}) => {
    // here we retrieve a two-dimensional Array of all Object entries,
    // in [key, value] arrays:
    const entries = Object.entries(obj);

    // if the length of entries in the Array of entries is zero we have
    // an empty Object (so no point continuing):
    if (entries.length === 0) {
      // returning false from the function:
      return false;
    }
   // otherwise we use Array.prototype.map() create a new Array:
   return entries.map(
    // using destructuring to assign the first array-element
    // to the (unused) 'key' variable, and the second (property-value
    // for the current entry) to the 'val' variable; and return that
    // 'val' (property-value of the original Object):
    ([key,val]) => val)
      // we then filter that Array of values using Array.prototype.filter():
      .filter(
        // passing the val variable to the function (the property-value of
        // the original Object) to return a new Array of Object-values
        // which are Arrays (checking with Array.isArray):
        (val) => Array.isArray(val)
      // then iterating over the Array-values using Array.prototype.every():
      ).every(
        // here we're checking if the Array-length is exactly zero:
        (val) => val.length === 0
      // if every Array-element is empty, so the length of every Array-element
      // is exactly zero then Array.prototype.every() returns Boolean true,
      // which is returned from the function:
      );
  };

console.log(arrayValuesEmpty(test));

References:

CodePudding user response:

An option that would check for array and non null / undefine could use the object.values & filter like this

Object.values: Returns all the values of an object

const is_empty : boolean = (item) => !Object.values(item).filter(n => (Array.isArray(n) && n.length > 0) || n === null || n === undefined ).length > 0

CodePudding user response:

Similar to the other answers, with the addition of checking for an empty object.

const pass1 = {};
const pass2 = { 123: [] }
const pass3 = { 123: [], 234: [] }
const fail1 = { 123: [], 234: [1] }

const isEmpty = (o) => !Object.keys(o).length || Object.values(o).every(a => !a.length);

[pass1,pass2,pass3,fail1].forEach(item => console.log(isEmpty(item)));

0 is falsey, so a.length is enough, you don't need to check a.length == 0.

CodePudding user response:

You can get the values by Object.values, and loop through and check if the value is an array, then check the length of the array equal to zero, if not check if the value equal null or undefined

const test = {
  "106596": [],
  "107014": [],
};

const test2 = {
  "106596": [5],
  "107014": [],
};

const test3 = {
  "106596": [],
  "107014": [],
  "121123": 'something',
};


function checkEmpty(value) {
  return Object.values(value).every(element => Array.isArray(element) ? element.length === 0 : [null, undefined].includes(element));
}
 

console.log(checkEmpty(test));  // true
console.log(checkEmpty(test2)); // false
console.log(checkEmpty(test3)); // false

CodePudding user response:

  const test = {
    "106596": [],
    "107014": []
  };
  
  const getKeys = Object.keys(test);
  const checkVal = getKeys.map((item)=> test.[item].length);
  const result = checkVal.some(e => e)
  console.log(result)

You can do this in one step instead of this, hope this would help

CodePudding user response:

I think the fastest way is a const or function that checks if Object is empty and return true or false:

const objChecker = Object.entries(clientData).length>0 ? true : false

Hope the best, Mauro

CodePudding user response:

const test = {
  one: [],
  two: [],
  three: undefined,
  hasVal: "Has Value!!",
  nullVal: null,
};

// loop
// you can ignore key
for (const [key, val] of Object.entries(test)) {
  // condintion
  if (val !== null && val !== undefined && val.length != 0) {
    console.log(true);
  }
}

  • Related