如何在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中循环嵌套对象?
答案 0 :(得分:1)
我已经为您准备了sample。主要思想是必须使用map函数来获取嵌套对象,然后对其进行迭代。
nestedItems = Object.keys(this.items).map(key => {
return this.items[key];
});
答案 1 :(得分:1)
<!-- 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;
}
}
}