How to iterate through nested object in Javascript?

Viewed 122
{
"ret": [],
"retUnderAgt": [],
"sum": {
  "services": [
    {
      "serviceCode": "BET",
      "serviceName": "Bet Games Lottery ",
      "txnTypes": [
        {
          "txnTypeName": "Sale",
          "amount": "0",
          "txnTypeCode": "SALE",
          "amt": "0.0"
        },
        {
          "txnTypeName": "Winning",
          "amount": "0",
          "txnTypeCode": "WIN_CLAIM",
          "amt": "0.0"
        }
      ]
    }]
}

}

I am trying to loop over sum like this ----------->>>

for(let i in activityDetailedReportData.sum){
  this.finalTotal.push("Total");
  
  for(let j of i.services){
    for(let k of j.txnTypes){
      this.finalTotal.push(k.amount)
    }
  } 
}

I am trying to add all amounts in all txnTypes into one array called finalTotal.

I don't know how to loop over txnTypes inside services. Tell me how should I do it?

I've edited the question as needed Please help me out in this

1 Answers

There's no need for the outer loop in your code, since you just want to use the single activityDetailedReportData.sum.services property:

this.finalTotal.push("Total");
for (let j of activityDetailedReportData.sum.services) {
    for (let k of j.txnTypes) {
         this.finalTotal.push(k.amount);
    }
} 

Live example (using finalTotal rather than this.finalTotal):

const activityDetailedReportData = {
    "ret": [],
    "retUnderAgt": [],
    "sum": {
      "services": [
        {
          "serviceCode": "BET",
          "serviceName": "Bet Games Lottery ",
          "txnTypes": [
            {
              "txnTypeName": "Sale",
              "amount": "0",
              "txnTypeCode": "SALE",
              "amt": "0.0"
            },
            {
              "txnTypeName": "Winning",
              "amount": "0",
              "txnTypeCode": "WIN_CLAIM",
              "amt": "0.0"
            }
          ]
        }]
    }
};

const finalTotal =[];

finalTotal.push("Total");
  
for (let j of activityDetailedReportData.sum.services) {
    for (let k of j.txnTypes) {
         finalTotal.push(k.amount);
    }
} 

console.log(finalTotal);


FWIW, that could probably benefit from destructuring:

this.finalTotal.push("Total");
for (let {txnTypes} of activityDetailedReportData.sum.services) {
    for (let {amount} of txnTypes) {
         this.finalTotal.push(amount);
    }
} 

Live Example:

const activityDetailedReportData = {
    "ret": [],
    "retUnderAgt": [],
    "sum": {
      "services": [
        {
          "serviceCode": "BET",
          "serviceName": "Bet Games Lottery ",
          "txnTypes": [
            {
              "txnTypeName": "Sale",
              "amount": "0",
              "txnTypeCode": "SALE",
              "amt": "0.0"
            },
            {
              "txnTypeName": "Winning",
              "amount": "0",
              "txnTypeCode": "WIN_CLAIM",
              "amt": "0.0"
            }
          ]
        }]
    }
};

const finalTotal =[];

finalTotal.push("Total");
for (let {txnTypes} of activityDetailedReportData.sum.services) {
    for (let {amount} of txnTypes) {
         finalTotal.push(amount);
    }
} 

console.log(finalTotal);

Related