In RxJS I want to convert an array that I have at some point into a sequence of items that are in the array. I found two ways to do it: Option 1 & 2, which I guess, do the same thing:
const obj = { array: [1, 2, 3, 4, 5] };
const observable = Observable.of(obj);
// Option 1
observable.flatMap(x => {
return Observable.from(x.array);
}).subscribe(console.log);
// Option 2
observable.flatMap(x => x.array).subscribe(console.log);
// Option 3 ?
Are there nicer / better ways that express what I'm doing , I mean without flatMap
operator?
I think you've pretty much reached the shortest possible way. The only improvement I might suggest is avoid using callback functions at all:
const obj = { array: [1, 2, 3, 4, 5] };
const observable = Observable.of(obj);
observable
.pluck('array')
.concatAll() // or mergeAll()
.subscribe(console.log);
See live demo: https://jsbin.com/zosonil/edit?js,console
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