filter

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

Emit values that pass the provided condition.


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


Examples

Example 1: filter for even numbers

( jsBin | jsFiddle )

//emit (1,2,3,4,5)
const source = Rx.Observable.from([1,2,3,4,5]);
//filter out non-even numbers
const example = source.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

( jsBin | jsFiddle )

//emit ({name: 'Joe', age: 31}, {name: 'Bob', age:25})
const source = Rx.Observable.from([{name: 'Joe', age: 31}, {name: 'Bob', age:25}]);
//filter out people with age under 30
const example = source.filter(person => person.age >= 30);
//output: "Over 30: Joe"
const subscribe = example.subscribe(val => console.log(`Over 30: ${val.name}`));
Example 3: filter for number greater than specified value

( jsBin | jsFiddle )

//emit every second
const source = Rx.Observable.interval(1000);
//filter out all values until interval is greater than 5
const example = source.filter(num => num > 5);
/*
  "Number greater than 5: 6"
  "Number greater than 5: 7"
  "Number greater than 5: 8"
  "Number greater than 5: 9"
*/
const subscribe = example.subscribe(val => console.log(`Number greater than 5: ${val}`));

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/filter.ts

results matching ""

    No results matching ""