Basit
Basit

Reputation: 17184

Angular2 RxJs map http response

I have api which returns list of photos object, which I want to iterate using subscribe().. not all at once, but one by one..

http.get('photos.json')
      .map(res => res.json());

Which functions (map, flatmap...) can I use to convert response into multiple array, so when using subscribe, it will iterate one by one.. and not all response at once.

json example file

Upvotes: 3

Views: 3110

Answers (2)

Sasxa
Sasxa

Reputation: 41254

You can use flatMap:

http.get('photos.json')
  .map(res => res.json())
  .flatMap((array, index) => array)
  .filter(photo => 600 <= photo.height)
  .subscribe(photo => console.log(photo))

Upvotes: 2

kakigoori
kakigoori

Reputation: 1228

Flatmap will do it, if you create an Observable out of the resulting array.

yourPhotosArray.flatMap(photos => Observable.from(photos))

will convert your array of photos into Observable

Upvotes: 2

Related Questions