const whyYouDoThisObject = {
a: 1,
b: {
c: 2,
d: {
e: {
a: 3,
b: 4,
c: {
c: 5,
},
},
},
},
f: 6,
};
 
function deepCountProperties(obj, property) {
let count = 0;
for (let key in obj) {
if (key === property) {
count += 1;
}
if (typeof obj[key] === 'object') {
count += deepCountProperties(obj[key], property);
}
}
return count;
}
console.log(deepCountProperties(whyYouDoThisObject, 'c')); // 3
Click Run to execute JavaScript.
Code runs in an isolated environment.