filter

signature: filter(select: Function, thisArg: any): Observable

Emit values that pass the provided condition.


💡 If you would like to complete an observable when a condition fails, check out takeWhile!


Ultimate RxJS

Examples

Example 1: filter for even numbers

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

//emit (1,2,3,4,5)
const source = from([1, 2, 3, 4, 5]);
//filter out non-even numbers
const example = source.pipe(filter(num => num % 2 === 0));
//output: "Even number: 2", "Even number: 4"
const subscribe = example.subscribe(val => console.log(`Even number: ${val}`));

Example 2: filter objects based on property

( StackBlitz | jsBin | jsFiddle )

Example 3: filter for number greater than specified value

( StackBlitz | jsBin | jsFiddle )

Additional Resources


📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/filter.ts

Last updated