BACK
Referencefilter
Reference · 13

filter

Lets through only values that satisfy the predicate.

Signature

filter(predicate: (value: T, index: number) => boolean): OperatorFunction

Marble diagram

in:           --1--2--3--4--|
filter(even): -----2-----4--|

What it does

For each next it calls predicate and passes the value on only when the result is true. All other values are dropped.

When to use

Selecting events (was Enter pressed), validation (length > 2), rejecting null/undefined. It can act as a type guard (filter((v): v is X => v !== null)) — TypeScript narrows the type after the operator.

See also

take, distinctUntilChanged, skip

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...