I have the following function which resets an object with variable depth's deepest value to 0. I want the function to change the property of the object outside the function's scope;
var object =
{ '1': {
'10000': { '0': 6, '15': 3, '35': 5, '55': 3, '75': 85 },
}
}
function resetCounts(obj) {
for (var el in obj) {
if (obj.hasOwnProperty(el)) {
if (typeof obj[el] == 'number') {
if (obj[el] > 0) {
console.log(obj)
console.log(obj[el]);
obj[el] = 0;
console.log('reset');
console.log(obj);
console.log(obj[el]);
}
return;
}
resetCounts(obj[el]);
}
}
}
resetCounts(object);
Here is the result in the console:
{ '0': 6, '15': 3, '35': 5, '55': 3, '75': 85 }
6
reset
{ '0': 0, '15': 3, '35': 5, '55': 3, '75': 85 }
0
The expected result is for object to be:
{ '1': {
'10000': { '0': 0, '15': 0, '35': 0, '55': 0, '75': 0 },
}
}
Any idea how this can be achieved? How can I pass the value to the parent object?
returnstatement? It's halting your loop after the first number is found. Simply remove it and use anelseif you don't want a recursive call to happen.