I am trying to get a tally of the keys in nested JS objects. I was able to get to the first level, but I'm a little stuck on how I would have it dig into the nested objects and return a count.
var properties = {
prop1: '',
prop2: '',
prop3: '',
prop4: {
subProp1: '',
subProp2: '',
subProp3: {
subSubprop1: ''
}
}
}
var getCount = function (data) {
var count = 0;
for (var k in data) {
if (properties.hasOwnProperty(k)) {
++count;
}
}
console.log( "this is the count for level 0: " + count //returns 4);
console.log( "this is the count for level 1: " + count //should return 3);
console.log( "this is the count for level 2: " + count //should return 1);
return count;
}
getCount(properties);