RxJS has a wealth of operators that perform FlatMap-like operations. In RxJS, the functions that transform items emitted by the source Observable into Observables typically take as parameters both the item and the index of the item in the Observable sequence.
RxJS implements the basic flatMap
operator. It has a variant that allows you to apply a transformative function (an optional second parameter to flatMap
) to the items emitted by the Observables generated for each item in the source Observable, before merging and emitting those items.
flatMap
works just as well if the function you provide transforms items from the source Observables into Observables, into Promises, or into arrays.
“selectMany
” is an alias for flatMap
.
var source = Rx.Observable .range(1, 2) .selectMany(function (x) { return Rx.Observable.range(x, 2); }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 1 Next: 2 Next: 2 Next: 3 Completed
// Using a promise var source = Rx.Observable.of(1,2,3,4) .selectMany(function (x, i) { return Promise.resolve(x + i); }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 1 Next: 3 Next: 5 Next: 7 Completed
// Using an array Rx.Observable.of(1,2,3) .flatMap( function (x, i) { return [x,i]; }, function (x, y, ix, iy) { return x + y + ix + iy; } ); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 2 Next: 2 Next: 5 Next: 5 Next: 8 Next: 8 Completed
flatMap
is found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
The flatMapLatest
operator behaves much like the standard FlatMap operator, except that whenever a new item is emitted by the source Observable, it will unsubscribe to and stop mirroring the Observable that was generated from the previously-emitted item, and begin only mirroring the current one.
“selectSwitch
” is an alias for flatMapLatest
.
var source = Rx.Observable .range(1, 2) .flatMapLatest(function (x) { return Rx.Observable.range(x, 2); }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 1 Next: 2 Next: 3 Completed
flatMapLatest
is found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
flatMapObserver
creates (and flattens) a new Observable for each item and notification from the source Observable. It accepts a different transformation function to respond to onNext
, onError
, and onCompleted
notifications and to return an Observable for each.
“selectManyObserver
” is an alias for flatMapObserver
.
var source = Rx.Observable.range(1, 3) .flatMapObserver( function (x, i) { return Rx.Observable.repeat(x, i); }, function (err) { return Rx.Observable.return(42); }, function () { return Rx.Observable.empty(); }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 2 Next: 3 Next: 3 Completed
flatMapObserver
is found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
There is also a concatMap
operator, which is like the flatMap
operator, but it concatenates rather than merges the resulting Observables in order to generate its own sequence.
As with flatMap
, concatMap
works just as well if the function you provide transforms items from the source Observables into Observables, into Promises, or into arrays.
“selectConcat
” is an alias for concatMap
.
concatMap
is found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
The for
operator (and its alias, forIn
) is very similar to concatMap
, though it has a converse flexibility. While concatMap
operates on an Observable source and can use Observable, Promise, or array intermediaries to generate its output sequence; for
always uses Observables as its intermediaries, but can operate on a source that is either an Observable, a Promise, or an array.
concatMap
is found in each of the following distributions:
rx.all.js
rx.all.compat.js
rx.experimental.js
(requires either rx.js
, rx.compat.js
, rx.lite.js
, or rx.lite.compat.js
)There is also a concatMapObserver
operator, which is like the flatMapObserver
operator, in that it creates Observables to merge from both the emissions and terminal notifications of the source Observable, but it concatenates rather than merges these resulting Observables in order to generate its own sequence.
“selectConcatObserver
” is an alias for concatMapObserver
.
concatMapObserver
is found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
The manySelect
operator is often described as a “comonadic bind.” If that clears things up for you, you’re welcome. Elsewise, here’s an explanation:
manySelect
internally transforms each item emitted by the source Observable into an Observable that emits that item and all items subsequently emitted by the source Observable, in the same order. So, for example, it internally transforms an Observable that emits the numbers 1,2,3 into three Observables: one that emits 1,2,3, one that emits 2,3, and one that emits 3.
Then manySelect
passes each of these Observables into a function that you provide, and emits, as the emissions from the Observable that manySelect
returns, the return values from those function calls.
In this way, each item emitted by the resulting Observable is a function of the corresponding item in the source Observable and all of the items emitted by the source Observable after it.
manySelect
is found in each of the following distributions:
rx.all.js
rx.all.compat.js
rx.experimental.js
manySelect
requires one of the following distributions:
rx.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4