In my Angular application I use HttpClient to get json data from server. However, I need to do some conversion to the returned data at the client-side.
So I try to convert an array of Type A returned from the server to an array of Type B with the following code snippet.
this.http.get<any[]>(this.env.accounts.url.accountTypes).pipe(map(e => {
console.log(e); // print the retuned array not a single item
return {'label': e.name, 'value': e.id}
}
)).subscribe(data => {
this.accountTypes = data;
console.log(this.accountTypes); // prints {label: undefined, value: undefined}
});
I must be doing this wrong, but I cannot figure out what is wrong here.
The server returns an array like:
[
{name: "Name 1", id: 1},
{name: "Name 2", id: 2},
{name: "Name 3", id: 3},
]
And I need to convert it into the following format
[
{label: "Name 1", value: 1},
{label: "Name 2", value: 2},
{label: "Name 3", value: 3},
]
Can somebody please point out what is wrong in my code and how to fix it.
angularjsinstead ofangular, he has corrected it.