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

11
  • 2
    please add the object (a small subset) in text form. Commented Jul 1, 2020 at 7:13
  • 1
    Please also add the result you're currently getting and what you would expect instead Commented Jul 1, 2020 at 7: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) }) }) } Commented Jul 1, 2020 at 7:33
  • 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) }) }) Commented Jul 1, 2020 at 7:49
  • 1
    @T.J.Crowder Please check updated code Commented Jul 1, 2020 at 8:06

1 Answer 1

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);

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

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.