Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 232
Basit Avatar asked Mar 23 '26 22:03

Basit


2 Answers

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

like image 73
kakigoori Avatar answered Mar 26 '26 10:03

kakigoori


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))
like image 39
Sasxa Avatar answered Mar 26 '26 12:03

Sasxa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!