DEV Community

George Jempty
George Jempty

Posted on

Two ways to determine if any arrays in an object has an entry

let pendingAdditions = {
  hospitals: ['Silencio Hospital','St Judes'],
  licenses: ['poe-tic license'],
  medschools: []
}

let somePendingAdditions = Object.values(pendingAdditions).some(arr => arr.length);
console.log(somePendingAdditions); // true
somePendingAdditions = 
    Boolean(Array.prototype.concat.apply([], Object.values(pendingAdditions)).length);
console.log(somePendingAdditions); // true

Pitfalls regarding the second approach noted at: https://stackoverflow.com/questions/60042666/how-to-determine-if-any-arrays-in-an-object-has-an-entry/

Top comments (0)