function
stable
Filter items emitted by the source Observable by only emitting those that satisfy a specified predicate.
filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T>
predicate | A function that evaluates each value emitted by the source Observable. If it returns |
thisArg | Optional. Default is An optional argument to determine the value of |
MonoTypeOperatorFunction<T>
: An Observable of values from the source that were allowed by the predicate
function.
Like Array.prototype.filter(), it only emits a value from the source if it passes a criterion function.
Similar to the well-known Array.prototype.filter
method, this operator takes values from the source Observable, passes them through a predicate
function and only emits those values that yielded true
.
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));
© 2015–2018 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/filter