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.
source to share
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()
source to share
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!
source to share