For example, I have a JSON data as below from JSONPlaceholder website https://jsonplaceholder.typicode.com/users.
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "[email protected]",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}]
From this array of objects, I want array of objects with username and name as below:
[
{"name": "Leanne Graham",
"username": "Bret"},
{ "name": "Ervin Howell",
"username": "Antonette"
}
]
I am using Httpclient to access JSON data. My code in app.component.ts is as below:
interface user {
username : String,
name : String
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
users : Observable<user>
constructor(private _http : HttpClient){}
name = 'Angular ' + VERSION.major;
ngOnInit() {
this.users = this._http.get('https://jsonplaceholder.typicode.com/users')
I am accessing array of users with async pipe in app.component.Html as below:
<div *ngFor = "let user of users | async" >
{{user.Username}}
{{user.name}}
</div>
I have tried building type interface for entire json object and there by filtering with map operator but it is too much for a large amount of JSON data. Any other solution will be really helpful Thanks in advance.
mapoperator is the easiest way. Why not use it as it is?