Currently, I am faced with the task of summing a nested value for all objects within an object. The structure of my object is as follows:
const json = [
{
"other_sum": "1",
"summary": {
"calculations": {
"time": 10,
"unit": 25
},
"updated": "2020-06-05"
}
},
{
"other_sum": "1",
"summary": {
"calculations": {
"time": 20,
"unit": 5
},
"updated": "2020-06-05"
}
},
{
"other_sum": "1",
"summary": {
"calculations": {
"time": 5,
"unit": 15
},
"updated": "2020-06-05"
}
},
];
The objective is to calculate the total sum of all "unit" values present.
I have attempted using .reduce for this purpose, however, encountered an error when adding a third item to the object.
Below is the snippet of code I am currently working on:
const r = json.reduce((a, b) => a.summary.calculations.unit + b.summary.calculations.unit);
console.log(r);
Confused about what might be going wrong in my approach. Any help or guidance would be greatly appreciated.