如何从数组中获取结果?

时间:2019-10-30 04:29:49

标签: angular

我有这样的JSON:

{
  "count": 1,
    "rows": [
      {
        "hour_id": 1,
        "report_date": "2019-10-22",
        "teams_id": 5,
        "team": {
          "team_id": 5,
          "full_name": "trytrys",
          "is_active": true,
          "masters": [
            {
              "master_id": 11,
              "full_name": "sf",
              "comment": "gff",
              "date_work": "2019-06-22",
              "teams_id": 5
            },
            {
              "master_id": 10,
              "full_name": "dgdf",
              "comment": "hjgj",
              "date_work": "2019-10-05",
              "teams_id": 5
            }
          ]
        }
      }
    ]
}

我需要从该阵列的主服务器获取数据。为了能够进一步找到最大字段date_work。如何获得大师的数据?

ts:

  private load() {
    const params = {
      search: this.search,
      pageSize: this.pageSize,
      page: this.page
    };
    this.loading = true;
    this.isActive = false;
    this._homeService.fetch(this.report_date, params).subscribe(repHours => {
      this.loading = false;
      this.isActive = true;
      this.repHours = repHours.rows;
      this.totalItems = repHours.count;
    }, error => {
      this.loading = false;
      this.isActive = true;
      console.log(error)
    })
  }

html:

<div *ngFor="let repHour of repHours">
    {{repHour.team.masters.date_work}}
</div>

3 个答案:

答案 0 :(得分:0)

由于masters是数组格式,因此要访问内部属性,您需要添加嵌套的*ngFor,如下所示-

<div *ngFor="let repHour of repHours">
    <div *ngFor="let masters of repHour?.team?.masters">{{masters?.date_work}}</div>
</div>

答案 1 :(得分:0)

仅使用地图进行循环,例如:JSONDATA.rows.map(row => row.team.masters.map(master => console.log(master.date_work)));

签出:https://jsfiddle.net/35j4th89/3/

答案 2 :(得分:0)

new Date(Math.max.apply(null, a.rows.team.masters.map(function(e) {
         return new Date(e.date_works);
})));

通过这种方式,您可以获取最大日期。