【问题标题】:Not seeing console log data in for loop在 for 循环中看不到控制台日志数据
【发布时间】:2021-05-19 05:46:21
【问题描述】:

我有以下数据集,我正在尝试循环访问。我也有执行循环的功能,但运行应用程序时看不到控制台日志

{
    "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)
        }
      }
    })
}

知道为什么控制台日志不打印任何数据吗?

【问题讨论】:

  • @SiddAjmera 我在控制台中没有看到任何错误
  • @SiddAjmera 是的,我看到了。我需要另一种解决方案才能进入对象
  • 所以你需要calculatorResult数组中每个项目的所有属性的总和是吗?
  • @SiddAjmera 是的,这是正确的

标签: angular


【解决方案1】:

假设您总是将calculatorResult 作为一个数组获取,而我们只需要获取对象上第 0 个索引处的字段总和,您可以执行以下操作:

试试这个:

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

这是一个带有解决方案代码的 Working Sample StackBlitz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2019-08-18
    • 2014-02-08
    相关资源
    最近更新 更多