7

I'm making an Angular 2 HTTP get request and in return I get Observable<Message[]> I want to transform this observable into multiple emit. So let's say the server returned Message array with length 3. I would like to get 3 notification in my subscribe call (on for each value in the array) instead of getting one call with the array.

e.g : ['Hello','Hey','Howdy'] - > 'Hello', 'Hey','Howdy'

I found an operator that does transform the array (Observable.for), but this operator takes as an argument an array and not an Observable.

3 Answers 3

8

You can use the concatAll operator to flatten the array (mergeAll will work as well).

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

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

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

1 Comment

Thank you! Exactly what I was looking for.
3

Try this:

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

yourRequest() in this case should return array.

If yourRequest() returns Observable then:

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

1 Comment

since msgList carries over to Observable.from, flatMap(msgList => Observable.from(msgList)) can be written as ` .flatMap(Rx.Observable.from) `
0

If I understand you well, you want to use something like concatMap. By using concatMap, you can map values(Message array in your case) to inner observable, and then you can subscribe these values in order.

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

You can also take a look at this page.

I hope it helps!

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.