javascript

How to Check if Arrays in a Object Are All Empty

var allEmpty = Object.keys(obj).every(function(key){
return obj[key].length === 0
})

//filter 
function isUnPopulatedObject(obj) { 
    return Object.keys( obj ).filter( function(key){
      return obj[ key ].length > 0;  //if any array has a property then filter will return this key.
    }).length == 0; //used == here since you want to check if all are empty
}

//every 
function isUnPopulatedObject(obj) { 
    return Object.keys( obj ).every( function(key){
      return obj[ key ].length == 0;  
    }); 
}

//some 
function isUnPopulatedObject(obj) { 
    return Object.keys( obj ).some( function(key){
      return obj[ key ].length > 0;  
    }) === false; 
}

//es8 
const objectIsEmpty = obj => Object.values(obj).every(
    val => Array.isArray(val) && val.length === 0
);

//every 2 sample 
function objectIsEmpty(obj) {
    return Object.keys(obj).every(key => 
      Array.isArray(obj[key]) && obj[key].length
    );
}
Output
How to Check if Arrays in a Object Are All Empty

How to Check if Arrays in a Object Are All Empty

Was this helpful?