3

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

3
  • You want to iterate the images, right? Commented Feb 21, 2016 at 23:47
  • Kind of yes, but not by passing complete images to angular.. but make them obversable.. so we can apply more filters, before iterating over them. Commented Feb 21, 2016 at 23:49
  • 1
    You already have the answer. You just have to import it from rxjs/add/operator/mergeMap (flatMap is an alias of mergeMap), see github.com/ReactiveX/RxJS/blob/master/src/add/operator/… Commented Feb 22, 2016 at 0:31

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.