How can I loop over and check if one or some arrays inside of a JavaScript Object is empty?
i.e.
const store = {
shirts : [],
shoes: ['nike', 'puma', 'addidas']
hats: [],
jerseys: ['barcelona', 'milan', 'arsenal']
}
Get the values of the object using Object.values(). Check if at least one of them is an array with the length of 0 using some and Array.isArray()
const store = {
shirts: [],
shoes: ['nike', 'puma', 'addidas'],
hats: [],
jerseys: ['barcelona', 'milan', 'arsenal']
}
console.log(
Object.values(store).some(v => Array.isArray(v) && v.length === 0)
)
If you want to get the empty keys than you can use filter method
const store = {
shirts : [],
shoes: ['nike', 'puma', 'addidas'],
hats: [],
jerseys: ['barcelona', 'milan', 'arsenal']
}
const emptyKeys = Object.keys(store).filter((key) => {
if (store[key].length == 0) {
return key;
}
});
console.log(emptyKeys);