Ich habe einen Beispiel-JSON mit dem Namen SampleJSON
wie nachstehend:
"type1":{
"0":{"title":"Title1","url":"URL1"},
"1":{"title":"Title2","url":"URL2"},
"2":{"title":"Title3","url":"U",
"0":{"title":"Title1","url":"URL1"},
"1":{"title":"Title2","url":"URL2"},
-----------------------------------,
-----------------------------------
}
"3":{"title":"Title4","url":"URL4"},
-----------------------------------,
-----------------------------------
}
Wie kann ich den Knoten durchlaufen "type1"
in Angular5 und erhalte den Wert für title
& url
. Ich stecke fest und kann mich nach dem Versuch nicht weiter bewegen ngFor
.
Antworten:
0 für die Antwort № 1Sie können nur über Tasten loopen
Versuche dies:
https://stackblitz.com/edit/angular-y1wlkt?file=app%2Fapp.component.ts
export class AppComponent {
name = "Angular 5";
test: any;
testFinal: any[];
constructor() {
this.testFinal = [];
this.test = {
"type1": {
"0": { "title": "Title1", "url": "URL1" },
"1": { "title": "Title2", "url": "URL2" },
"2": {
"title": "Title3", "url": "U",
"0": { "title": "Title1", "url": "URL1" },
"1": { "title": "Title2", "url": "URL2" },
},
"3": { "title": "Title4", "url": "URL4" }
}
}
//Iterate the keys
for(let x in this.test){
for(let y in this.test[x]){
this.testFinal.push(this.test[x][y]);
}
}
console.log(this.testFinal)
}
}