Filter an Observable using RxJS

filter objects based on property

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

//emit ({name: 'Joe', age: 31}, {name: 'Bob', age:25})
const source = from([{ name: 'Joe', age: 31 }, { name: 'Bob', age: 25 }]);
//filter out people with age under 30
const example = source.pipe(filter(person => person.age >= 30));
//output: "Over 30: Joe"
const subscribe = example.subscribe(val => console.log(`Over 30: ${val.name}`));

References
https://www.learnrxjs.io/operators/filtering/filter.html
https://rxjs.dev/api/operators/filter

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.