const whyYouDoThisObject = {
a: 1,
b: {
c: 2,
d: {
e: {
a: 3,
b: 4,
c: {
c: 5,
},
},
},
},
f: 6,
};
let count = 0;
function deepCountProperties(obj, property) {
for (const key in obj) {
const value = obj[key];
console.log({ value });
if (typeof value !== 'number') {
deepCountProperties(value, property);
} else if (key === property) {
count += 1;
}
}
return count;
}
console.log(deepCountProperties(whyYouDoThisObject, 'a'));
console.log({ count });
Code will run automatically after changes.
Code runs in an isolated environment.
Code runs in an isolated environment.