function
deprecated
Splits the source Observable into two, one with values that satisfy a predicate, and another with values that don't satisfy the predicate.
use partition
static creation function instead
partition<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): UnaryFunction<Observable<T>, [Observable<T>, Observable<T>]>
predicate | A function that evaluates each value emitted by the source Observable. If it returns |
thisArg | Optional. Default is An optional argument to determine the value of |
UnaryFunction<Observable<T>, [Observable<T>, Observable<T>]>
: An array with two Observables: one with values that passed the predicate, and another with values that did not pass the predicate.
It's like filter
, but returns two Observables: one like the output of filter
, and the other with values that did not pass the condition.
partition
outputs an array with two Observables that partition the values from the source Observable through the given predicate
function. The first Observable in that array emits source values for which the predicate argument returns true. The second Observable emits source values for which the predicate returns false. The first behaves like filter
and the second behaves like filter
with the predicate negated.
Partition click events into those on DIV elements and those elsewhere
import { fromEvent } from 'rxjs'; import { partition } from 'rxjs/operators'; const clicks = fromEvent(document, 'click'); const parts = clicks.pipe(partition(ev => ev.target.tagName === 'DIV')); const clicksOnDivs = parts[0]; const clicksElsewhere = parts[1]; clicksOnDivs.subscribe(x => console.log('DIV clicked: ', x)); clicksElsewhere.subscribe(x => console.log('Other clicked: ', x));
© 2015–2018 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/partition