RxJS Observed return array, run another function with each iteration of the array

I have a function getNews()that basically returns an angular http.get request. The query result is an array of identifiers. I would like to iterate through this array that I received and run another http.get (function getItem(id)) request , which will then return one Id object received from the server.

I tried using it like this:

  getLatest() {
    return this.http.get('all_news_url')
    .map(res => res.json())
    // I even tried creating Observable from array and get only 5 elements
    //.map(res => Observable.from(res.json()))
    //.take(5)
    .map((data) => this.getItem(data))
    .subscribe(
      data => {
        console.log(data);
      }
    )
  }

  getItem(itemId: any): Observable<any> {
    console.log('item id', itemId);

    return this.http.get(this.generateUrl(`item/${itemId}`))
    .map(res => res.json());
  }

Obviously this does not work. the parameter to the function is getItem()always passed as a whole array of Id.

Thank you all for participating in this matter.

+4
source share
1 answer

, concatMap . concatMap , .

, getItem() , 'all_news_url', -

this.http.get('all_news_url')
.concatMap(res => Observable.from(res.json()).take(5))
.concatMap((data) => this.getItem(data))
.subscribe(
  data => {
    console.log(data);
  }
);

getItem() , .. "item/${itemId}"

, , concatMap mergeMap. , ( item/itemId) - , . mergeMap ( , undefined null).

.concatMap(res => Observable.from(res.json()).take(5))
.mergeMap((data) => this.getItem(data), null, 3)

1 mergeMap concatMap

+6

All Articles