I have a deeply nested object with children. Every parent has a key of average. The average should be the average of all the total_value from all its children and subchildren. How can I make this work?
At the moment I'm only able to loop the whole object using a recursive function
_.each(data, function(parent) {
if(parent.__children.length > 0) {
....
}
});
const data = [{
category: "name-1"
average: 0,
__children: [
{
category: "name-1.1",
average: 0
total_value: 5,
__children: [{
category: "name-2.1",
average: 0
total_value: 2,
__children: []
}]
}, {
category: "name-1.2",
total_average: 0,
total_value: 10,
__children: [{
category: "name-2.2",
average: 0
total_value: 2,
__children: []
}]
}
]
}];