Eu tenho uma amostra JSON chamada SampleJSON
como abaixo:
"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"},
-----------------------------------,
-----------------------------------
}
Como posso percorrer o nó "type1"
em Angular5 e obtenha o valor para title
E url
. Estou preso e não posso avançar mais depois de tentar ngFor
.
Respostas:
0 para resposta № 1Você pode simplesmente fazer um loop via teclas
tente isto:
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)
}
}