简体   繁体   English

在Rx.js中进行过滤时,如何将可观察对象分成2个?

[英]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. 我想在过滤时将Rx.js可观察对象拆分为其他两个可观察对象,以便一个可观察对象包含已过滤的结果,另一个可观察对象包含其他所有内容。

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 . 我相信您需要的是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 . 可以在learnerxjs.io中找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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