2

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

4 Answers 4

4

You can use reduce method which accepts a callback method.

Also, use forEach method in order to iterate data items.

var 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
           }
          }
    ]

}]
}
json.data.forEach(function(item){
  var sum=item.jobTasks.reduce(function(sum,elem){
     return sum+elem.cost.amountString;
  },0);
   console.log('jobNumber'+item.jobNumber+' '+sum);
});

Sign up to request clarification or add additional context in comments.

Comments

3

You can do it using Array#map() to create a new array and Array#reduce() to sum the amountString

const apiJson = {"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}}]}]};

const output = apiJson.data.map(d=>({
  jobNumber : d.jobNumber,
  tasksCost : d.jobTasks.reduce((a,b)=>a.cost.amountString+b.cost.amountString)
}));

console.log(output);

Comments

1

first Update your json , "}" is missing from jobTasks of second object of array data :

   "jobTasks": [    { "id": 14,
                    "cost": {
                         "amountString": 100 
                           }
                     },
                     {
                      "id": 15,
                  "cost": {
                        "amountString": 200
                          }
                       }
                ]

Now To get Output:

i = 0,1

item.jobTasks[i]cost.amountString;

Comments

0

Here is a solution using object-scan. We use it for most of our data processing now. It does take a moment to wrap your head around though as it is versatile.

// const objectScan = require('object-scan');

const getCounts = (data) => objectScan(['data[*].jobTasks[*].cost.amountString'], {
  filterFn: ({ parents, value, context }) => {
    const jobNumber = parents[3].jobNumber;
    context[jobNumber] = (context[jobNumber] || 0) + value;
  }
})(data, {});

const apiJson = { 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 } }] }] };

console.log(getCounts(apiJson));
// => { '1': 600, '2': 300 }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>

Disclaimer: I'm the author of object-scan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.