【问题标题】:how to make a loop on a json file?如何在 json 文件上创建一个循环?
【发布时间】:2021-11-24 18:57:31
【问题描述】:

我必须处理一个 JSON 文件。

在 JSON BOCAGES -> BLOCAGE > 我必须检索 QTEDTE

这是我的文件 JSON here

我的问题是,我显示QTEDTE 一次。

现在,我有这个 => enter image description here

我想得到这个结果 => enter image description here

我缺少数组元素 [1][2],如何从循环中添加它们?

这是我的方法prepareDataForBlocage()

prepareDataForBlocage(response) {
    this.spinners.blockDetails = false;
    if (response['RETURNCODE'] == "OKK00") {


        let [variable1] = response['BLOCAGES']['BLOCAGE'];

        this.statusLine = {
            quantity: variable1['QTE'],
            update: variable1['DAT'],

        };

    }
}

我的 HTML

<table class="table table-striped">
   <tr style="background-color: #f8f9fa; font-weight: bold;">
      <td style="width: 13%;">Quantity</td>
      <td style="width: 13%;">Reason</td>
      <td style="width: 13%;">Date</td>
      <td style="width: 13%;">Référence</td>
   </tr>
   <tr  *ngIf="statusLine.quantity != 0" >
      <td>
         {{statusLine.quantity | number:'1.2-2' }}
      </td>
      <td></td>
      <td>
         {{statusLine.update | dateddmmyyyy }}
      </td>
      <td>
      </td>
   </tr>
</table>

【问题讨论】:

  • 使用行数组,而不是单个解构元素。

标签: javascript json angular typescript


【解决方案1】:

我发现您的代码中有 2 个问题

第一个:

let [variable1] = response['BLOCAGES']['BLOCAGE'];
/// is the same as
let variable1 = response['BLOCAGES']['BLOCAGE'][0];
/// That means you only get the first element of your list

第二个是不要在模板中循环遍历数组。

你可以试试这样的:

 this.myList = response['BLOCAGES']['BLOCAGE']
  .filter(blocage => blocage['QTE'] != 0)
  .map(blocage => ({
    quantity: blocage['QTE'],
    update: blocage['DAT'],
  }));
 <tr *ngFor="let elem of myList" >
      <td>
         {{elem.quantity | number:'1.2-2'  }}
      </td>
      <td></td>
      <td>
         {{elem.update | dateddmmyyyy  }}
      </td>
      <td>
      </td>
   </tr>

【讨论】:

  • 谢谢,}); 有语法错误?
  • 我忘记了括号 => }));
猜你喜欢
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多