I would like to take an Observable<T[]> and convert it to an Observable<T> such that each array from the Observable<T[]> is broken up and the individual elements of the arrays are then emitted, separately, via the Observable<T>.
Is there a standard operator for doing this? I've searched around but haven't found anything. Thanks.
After being pointed in the direction of concatMap/flatMap, I came up with the following general solution:
var source: Observable<T[]>;
...
var splitSource = source.flatMap<T>((x:T[]) => { return Rx.Observable.fromArray(x); });
You could use concatMap
like this:
function identity (x) {return x}
var obs_of_array$ = Rx.Observable.return(['1','2','you got it'])
.concatMap(identity)
This works because concatMap
also accepts arrays (and observables and promises) as the return value of the function selector that you pass as a parameter. Jsbin here, documentation here
So the array passed as parameter becomes the return value from the selector function, and then is flattened by the concatMap
operator while respecting the ordering of the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With