I'm looking for a method to deeply determine whether or not an object consists of nothing but empty objects.
For example, that method should return true for each one of the following objects:
const obj1 = {};
const obj2 = {
a: {}
b: {}
c: {}
};
const obj3 = {
a: {
a1: {}
},
b: {
b1: {},
b2: {}
},
c: {
c1: {},
c2: {},
c3: {}
}
};
And it should return false for the following object:
const obj4 = {
a: {
a1: {}
},
b: {
b1: {},
b2: {}
},
c: {
c1: {},
c2: {},
c3: {
x: 5
}
}
};
The simplest method I came up with is:
function isEmpty(object) {
return typeof object === 'object' &&
Object.keys(object).every(key => isEmpty(object[key]));
}
I couldn't find any counterexamples, so I'd like to ask if there are any flaws in this method.