3
{
"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

  • 2
    please add the object (a small subset) in text form. – Nina Scholz Jul 01 '20 at 07:13
  • 1
    Please also add the result you're currently getting and what you would expect instead – Ben Jul 01 '20 at 07:18
  • Not sure what exactly you are trying to do, but if I got your question correctly try this if you want to push all amounts in one array for(let service in activityDetailedReportData.sum){ service.forEach(txnTypes => { txnTypes.forEach(elem => { this.finalTotal.push(elem.amount) }) }) } – Bhuwan Pandey Jul 01 '20 at 07:33
  • @BhuwanPandey there's an error in this : Property 'forEach' does not exist on type 'string' – Nehal Jaisalmeria Jul 01 '20 at 07:38
  • 1
    @T.J.Crowder yes atleast a json will be helpful here instead of sccreenshot meanwhile I could understand, corrected the code const services = activityDetailedReportData.sum.services; services.forEach(service => { service.txnTypes.forEach(elem => { this.finalTotal.push(elem.amount) }) }) – Bhuwan Pandey Jul 01 '20 at 07:49
  • @BhuwanPandey - No reason to replace the OP's `for-of`s with `forEach`es. :-) – T.J. Crowder Jul 01 '20 at 07:51
  • @T.J.Crowder I personally prefer and suggest to use forEach because of the below reasons Better Readability Fewer off-by-one errors Maintainable as it may be easier to identify what the code is doing No variable setup – Bhuwan Pandey Jul 01 '20 at 07:59
  • @BhuwanPandey - All due respect, I have to disagree (fairly strongly) with all of those. :-) – T.J. Crowder Jul 01 '20 at 08:03
  • 1
    @T.J.Crowder Please check updated code – Nehal Jaisalmeria Jul 01 '20 at 08:06
  • @BhuwanPandey Please check updated question – Nehal Jaisalmeria Jul 01 '20 at 08:09
  • I also want to push a string called "Total" at the beginning of the array – Nehal Jaisalmeria Jul 01 '20 at 08:10

1 Answers1

1

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);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875