首页 > 解决方案 > 如何从数组中获取结果?

问题描述

我有这样的 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。怎么才能得到master上的数据?

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>

标签: angular

解决方案


只需使用 map 循环,例如:JSONDATA.rows.map(row => row.team.masters.map(master => console.log(master.date_work)));

看看:https ://jsfiddle.net/35j4th89/3/


推荐阅读