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?
Similar Posts
- Remove all false, null, undefined and empty values from array using Lodash
- Check if a variable is null, empty, undefined or false Javascript
- Methods to check empty array using javascript
- Get all keys from an object using Javascript
- Get all values as array items from an Object in Javascript
- Javascript to convert all object keys to lowercase
- Combine or merge two or multiple arrays into single array in Javascript