JSFiddle - React, Tailwind, and code Playground
by Diana Lemen
JavaScript
const nestedObjects = {
one:{
two: {
three: {
four: 'end'
}
}
}
}
let count = { count: 0 };
function countNested(obj, count) {
Object.entries(obj).forEach(([key, value]) => {
if(typeof value === 'object') {
count.count = count.count + 1;
countNested(value, count);
}
})
return count.count;
}
console.log(countNested(nestedObjects, count));