Angular HttpClient map observable array of objects

Matti Lehtinen Source

What is the simplest way to map each object of the result (Rxjs Observable) to an another object when querying an array of objects using angular HttpClient?

I'm currently doing it like below but is there a way to avoid "double mapping"?

In the example the end result needs to be Observable<Entry[]> with the constructor called for each Entry-object:

public getList(): Observable<Entry[]> {
  const url = "/entries/";
  return this.httpClient.get<Entry[]>(url)
    .map((entries: any[]) => => new Entry(e)));


answered 6 months ago funkizer #1

That's the best way, no shortcut to mapping both the observable and the array, if Entry has any other members than what you get from the api, or if some members need to be type converted (eg. Entry does something to the data in its constructor). If not though, no mapping is needed.

comments powered by Disqus