繁体   English   中英

如何在角度6中使用ngFor循环嵌套的json对象?

[英]How to loop a nested json object using ngFor in angular 6?

如何在angular6中循环嵌套的json对象? 我有一个嵌套的对象,如何在角度6中循环播放。 下面是json的获取:

{
  "62": {
    "0": {
      "0": "AbbVie's manufacturing facilities are in the following locations:",
      "1": "United States",
      "2": "Abbott Park, Illinois*",
      "3": "Barceloneta, Puerto Rico",
      "4": "Jayuya, Puerto Rico",
      "5": "North Chicago, Illinois",
      "6": "Worcester, Massachusetts*",
      "7": "Wyandotte, Michigan*"
    },
    "1": {
      "0": "",
      "1": "Outside the United States",
      "2": "Campoverde di Aprilia, Italy",
      "3": "Cork, Ireland",
      "4": "Ludwigshafen, Germany",
      "5": "Singapore*",
      "6": "Sligo, Ireland",
      "7": ""
    }
  },
  "68": {
    "0": {
      "0": "The following table lists AbbVie's executive officers, each of whom was first appointed as an AbbVie corporate officer in December 2012, except as otherwise indicated:",
      "1": "Name",
      "2": "Richard A. Gonzalez",
      "3": "Carlos Alban",
      "4": "William J. Chase",
      "5": "Henry O. Gosebruch*",
      "6": "Laura J. Schumacher",
      "7": "Michael E. Severino, M.D.*",
      "8": "Timothy J. Richmond",
      "9": "Azita Saleki-Gerhardt, Ph.D.",
      "10": "Robert A. Michael*"
    },
    "1": {
      "0": "",
      "1": "Age",
      "2": "64",
      "3": "55",
      "4": "50",
      "5": "45",
      "6": "54",
      "7": "52",
      "8": "51",
      "9": "54",
      "10": "47"
    },
    "2": {
      "0": "",
      "1": "Position",
      "2": "Chairman of the Board and Chief Executive Officer",
      "3": "Executive Vice President, Commercial Operations",
      "4": "Executive Vice President, Chief Financial Officer",
      "5": "Executive Vice President and Chief Strategy Officer",
      "6": "Executive Vice President, External Affairs, General Counsel and Corporate Secretary",
      "7": "Executive Vice President, Research and Development and Chief Scientific Officer",
      "8": "Senior Vice President, Human Resources",
      "9": "Senior Vice President, Operations",
      "10": "Vice President, Controller"
    }
  }
}

如何在角度6中循环嵌套对象?

我已经为您准备了样品 主要思想是必须使用map函数来获取嵌套对象,然后对其进行迭代。

nestedItems = Object.keys(this.items).map(key => {
  return this.items[key];
});
<!-- Iterate array -->
<div *ngFor="let obj of jsonData">
  <!-- iterate routes for each object using pipe -->
  <div *ngFor="let route of obj.routes | keys">
    {{route.toCity}}
  </div>
</div>
@Pipe({ name: 'keys',  pure: false })
export class KeysPipe implements PipeTransform {
    transform(value: any, args?: any[]): any[] {
      // check if "routes" exists
      if(value) {
        // create instance vars to store keys and final output
        let keyArr: any[] = Object.keys(value),
            dataArr = [];

        // loop through the object,
        // pushing values to the return array
        keyArr.forEach((key: any) => {
            dataArr.push(value[key]);
        });
        // return the resulting array
        return dataArr;
      }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM