9

I have created a Rx.Observable from a stream of events:

Rx.Observable.fromEvent(recognizeStream, 'data')

In which every data event looks like this:

{ error: null, alternatives: [result1, result2, result3] }

I want to pluck every value inside the array of alternatives and merge those into the stream. What operators do I have to look at?

As far as I know the flatMap and concatMap could do the job but I don't get the idea from their example.

Can somebody explain which operator i should use and provide me with an example?

Mark van Straten
  • 9,287
  • 3
  • 38
  • 57
lngs
  • 690
  • 1
  • 8
  • 17

3 Answers3

21

The family of xxxMap() operators all deal with higher-order Observables. Which means they allow you to create Observables inside the main Observable and inline the resulting values into the primary stream. So you could read the type signature as Observable<Observable<T>> => Observable<T>

Given a stream which every emission x is an Observable containing 4 value emissions:

input:  --x----------x 
flatMap   a-a-a-a-|  b-b-b-b-|
result: --a-a-a-a----b-b-b-b-|

Typecasting xxxMap(myFnc) return values

The xxxMap() operators all work with results of type Observable, Promise or Array. Depending on what you put into it will get converted to Observable if needed.

Rx.Observable.of('')
  .flatMap(() => [1,2,3,4])
  .subscribe(val => console.log('array value: ' + val));

Rx.Observable.of('')
  .flatMap(() => Promise.resolve(1))
  .subscribe(val => console.log('promise value: ' + val));

Rx.Observable.of('')
  .flatMap(() => Promise.resolve([1,2,3,4]))
  .subscribe(val => console.log('promise array value: ' + val));

Rx.Observable.of('')
  .flatMap(() => Rx.Observable.from([1,2,3,4]))
  .subscribe(val => console.log('Observable value: ' + val));
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.3/Rx.js"></script>

In your case you can easily flatMap the objects and return the arrays:

Rx.Observable.of({ error: null, alternatives: ['result1', 'result2', 'result3'] })
  .flatMap(val => val.alternatives)
  .subscribe(console.log);
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.3/Rx.js"></script>

Difference between all xxxMap operators

What does mergeMap do

flatMap, better known as mergeMap will merge all emissions whenever they come into the main stream.

enter image description here

concatMap

concatMap will wait for all emissions to complete before concat the next stream:

enter image description here

switchMap

but switchMap will abandon the previous stream when a new emission is available and switch to emit values from the new stream:

enter image description here

Mark van Straten
  • 9,287
  • 3
  • 38
  • 57
  • Your youtube link is broken :( – The Dembinski Jun 28 '17 at 05:27
  • 1
    Sorry to hear, i have updated my answer to clarify a bit about higher-order observables instead of linking to the missing movie – Mark van Straten Jun 28 '17 at 07:47
  • Am I right when guessing that concatMap(func) is the same as mergeMap(func, 1) (i.e. concurrency limited to 1)? ... Ah yes I'm right it's even mentioned in the docs :-), see http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-concatMap – NicBright Aug 16 '17 at 09:38
  • 1
    A really nice write-up, way better than the docs. You saved my day :) – JJWesterkamp Jan 13 '18 at 18:49
2

You should pluck "alternatives" then to iterate over the array use from, as from creates a new observable you need to flatten up it, so use you need flatMap to do the job. So finally you have alternatives back into the stream.

try it:

var data = {
  error: null,
  alternatives: [{
    result: 1
  }, {
    result: 2
  }, {
    result: 3
  }]
}

var input$ = Rx.Observable.of(data);
input$.pluck('alternatives').flatMap(alternatives => Rx.Observable.from(alternatives)).subscribe(alternative => console.log(alternative));
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.7/dist/global/Rx.umd.js"></script>
Victor Godoy
  • 1,642
  • 15
  • 18
1

Operators flatMap() and concatMap() are both a good choice. You can just turn the alternatives property to another Observable and then emit the array items merged into the stream.

const Observable = Rx.Observable;

Observable.of({ error: null, alternatives: ['result1', 'result2', 'result3'] })
  .concatMap(val => {
    return Observable.from(val['alternatives']);
  })
  .subscribe(val => console.log(val));

This prints to console:

result1
result2
result3

See live demo: https://jsbin.com/foqutab/2/edit?js,console

martin
  • 93,354
  • 25
  • 191
  • 226
  • What is the purpose of adding `return Observable.from(val['alternatives'])` inside `concatMap` rather than `return val.alternatives`. I had tried it and still be able to subscribe to the observable as well. – lngs Feb 03 '17 at 06:33
  • 1
    Returning `Observable.from` is universal and works with both RxJS 4 and RxJS 5. returning just an array works only in RxJS 5. – martin Feb 03 '17 at 08:58