emit only those items from an Observable that pass a predicate test
The Filter operator filters an Observable by only allowing items through that pass a test that you specify in the form of a predicate function.
filter ofType
RxGroovy implements this operator as filter
. You can filter an Observable, discarding any items that do not meet some test, by passing a filtering function into the filter
operator. For example, the following code filters a list of integers, emitting only those that are even (that is, where the remainder from dividing the number by two is zero):
numbers = Observable.from([1, 2, 3, 4, 5, 6, 7, 8, 9]); numbers.filter({ 0 == (it % 2) }).subscribe( { println(it); }, // onNext { println("Error: " + it.getMessage()); }, // onError { println("Sequence complete"); } // onCompleted );
filter
does not by default operate on any particular Scheduler.
filter(Func1)
There is also a specialized form of the Filter operator in RxGroovy that filters an Observable so that it only emits items of a particular class.
ofType
does not by default operate on any particular Scheduler.
ofType(Class)
filter ofType
RxJava implements this operator as filter
.
Observable.just(1, 2, 3, 4, 5) .filter(new Func1<Integer, Boolean>() { @Override public Boolean call(Integer item) { return( item < 4 ); } }).subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
filter
does not by default operate on any particular Scheduler.
filter(Func1)
There is also a specialized form of the Filter operator in RxJava that filters an Observable so that it only emits items of a particular class.
ofType
does not by default operate on any particular Scheduler.
ofType(Class)
filter where
RxJS implements this operator under two names, but with identical behavior: filter
and where
. This operator takes two parameters: the predicate function, and an optional object that will represent that function’s “this
” context when it executes.
The predicate function itself takes three arguments:
Write the predicate function so that it returns true
for those items you want to pass through the filter to the next observer, and false
for those items you want the filter to block and suppress.
var source = Rx.Observable.range(0, 5) .filter(function (x, idx, obs) { return x % 2 === 0; }); var subscription = source.subscribe( function (x) { console.log('Next: %s', x); }, function (err) { console.log('Error: %s', err); }, function () { console.log('Completed'); });
filter
and where
are found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
filter where
RxPHP implements this operator as filter
.
Emit only those items from an Observable that pass a predicate test.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/filter/filter.php $observable = Rx\Observable::fromArray([21, 42, 84]); $observable ->filter(function ($elem) { return $elem >= 42; }) ->subscribe($stdoutObserver);
RxPHP also has an operator where
.
Alias for filter
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/filter.html