I'm using the following code to query an API, which is working well to return nested values in JSON:
const obj = response.data.map(function(item) {
return [item.id, item.jobNumber];
});
Example JSON:
{
"data": [
{
"id": 100,
"jobNumber": 1,
"jobTasks": [
{
"id": 12,
"cost": {
"amountString": 100
},
{
"id": 13,
"cost": {
"amountString": 500
}
}
}
]
},
{
"id": 101,
"jobNumber": 2,
"jobTasks": [
{
"id": 14,
"cost": {
"amountString": 100
},
{
"id": 15,
"cost": {
"amountString": 200
}
}
}]
}]
}
I'm wanting to now loop through the nested Job Tasks, and SUM the item.jobTasks.cost.amountString for each job, So that the following could be returned:
- JobNumber1: Task Costs: 600
- JobNumber2: Task Costs: 300