Reputation: 2116
I have the below data set that I am trying to loop through. I also have my function thaty does the looping but I cannot see the console logs when i run th application
{
"header": {
"statuscode": "0",
},
"calculateAVAFLoanAdjustment": {
"responseCode": null,
"responseDescription": null,
"calculatorResults": {
"calculatorResult": [
{
"newCalculatedInstallmentsNo": "63",
"newContractEndDate": "20260725",
"newResidual": "24031.28",
"newInstalment": "5713.38",
"newTerm": "72",
"outBalanceAvaf": null,
"restructureType": "balloon"
}
]
}
}
}
calculateAVAFLoanAdjustment() {
this.avafService.CalculateAVAFLoanAdjustment({accountNumber: '555666'}).subscribe((resp)=>{
this.confirmData = resp.calculateAVAFLoanAdjustment;
for(let i = 0; i < this.confirmData; i++) {
this.calculatorResults = this.confirmData[i].calculatorResults;
console.log("calculatorResults: "+this.calculatorResults)
for(let j = 0; j < this.calculatorResults; j++) {
this.calculatorResult = this.confirmData[i].calculatorResults[j].calculatorResult;
console.log("calculatorResult: "+this.calculatorResult)
}
}
})
}
Any idea why the console logs do not print any data?
Upvotes: 1
Views: 117
Reputation: 39482
Assuming that you're always going to get calculatorResult
as an array and we just need to get the sum of the fields present on the Object at the 0th index, here's what you can do:
Try this:
calculateAVAFLoanAdjustment() {
this.avafService
.CalculateAVAFLoanAdjustment({
accountNumber: '555666'
})
.subscribe((resp: any) => {
this.calculatorResults =
resp.calculateAVAFLoanAdjustment.calculatorResults;
const [calculatorResult] = this.calculatorResults.calculatorResult;
this.calculatorResult = Object.keys(calculatorResult).reduce(
(acc, key) => {
const value = calculatorResult[key];
if (!isNaN(+value)) {
acc += +value;
}
return acc;
},
0
);
});
}
Here's a Working Sample StackBlitz with the solution code.
Upvotes: 1