Efficient-angular
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Filter

Filter items emitted by the source Observable by only emitting those that satisfy a specified predicate.

Syntax

filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T>
ParametersDescription
predicateA function that evaluates each value emitted by the source Observable. If it returns true, the value is emitted, if false the value is not passed to the output Observable. The index parameter is the number i for the i-th source emission that has happened since the subscription, starting from the number 0.
thisArg OptionalDefault is undefined. An optional argument to determine the value of this in the predicate function.

Returns

MonoTypeOperatorFunction: A function that returns an Observable that emits items from the source Observable that satisfy the specified predicate.

Example

Emit only click events whose target was a DIV element

import { fromEvent } from 'rxjs';
import { filter } from 'rxjs/operators';

const clicks = fromEvent(document, 'click');
const clicksOnDivs = clicks.pipe(filter(ev => ev.target.tagName === 'DIV'));
clicksOnDivs.subscribe(x => console.log(x));