RXJS Observable array of transformations for multiple values

I am making an Angular 2 HTTP get request and in return I get Observable<Message[]>

I want to convert this observable to multiple radiation. So, let's say the server returned an array of messages of length 3. I would like to receive 3 notifications in my subscribed call (for each value in the array) instead of getting one call with the array.

eg: ['Hello', 'Hey', 'Howdy'] β†’ 'Hello', 'Hey', 'Howdy'

I found an operator that converts an array (Observable.for), but that operator takes an array as an argument, not an Observable.

+3


source to share


3 answers


Try the following:

Observable.from(yourRequest())
    .flatMap(msgList => Observable.from(msgList))
    .subscribe(msg => console.log(msg));

      

yourRequest()

in this case should return an array.



If it yourRequest()

returns Observable

, then:

yourRequest().flatMap(msgList => Observable.from(msgList))
    .subscribe()

      

+1


source


You can use operator concatAll

to align the array ( mergeAll

will work too).

Observable.of(['Hello','Hey','Howdy'])
  .concatAll()
  .subscribe(console.log)

      



See demo: https://jsbin.com/gaxajex/3/edit?js,console

+4


source


If I understand you well, you want to use something like concatMap . Using concatMap

, you can map values ​​( Message

array in your case) to an internal observable and then you can subscribe to those values ​​in order.

yourRequest.concatMap(i: Message => Observable.of(i)).subscribe(i => {
     //sendNotification(i);
});

      

You can also see this page .

Hope this helps!

0


source







All Articles