简体   繁体   中英

How to split an observable into 2 when filtering in Rx.js?

I'd like to split an Rx.js observable into two other observables when filtering such that one observable contains the filtered results and the other contains everything else.

Is it possible to split them without creating two separate filters?

Instead of this:

const observable$ = Rx.Observable.of(1, 2, 3, 4)

observable$.filter(isOdd)
observable$.filter(isEven)

I'm looking for something like this:

const [isOdd$, isNotOdd$] = observable$.split(isOdd)

I believe what you need is partition .

const source = Rx.Observable.from([1,2,3,4,5,6]);

//first value is true, second false
const [evens, odds] = source.partition(val => val % 2 === 0);

evens.subscribe(n => console.log(n));
// 2, 4

More information can be found at learnrxjs.io .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM