For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/fp/filter.md.

filter (Functional Programming) es-toolkit

Creates a function that keeps only the elements that pass a test, equivalent to Array.prototype.filter. Use it with pipe.

const result = pipe(array, filter(predicate));
Info

This helper is specific to es-toolkit/fp. Use it when you want this operation as part of a pipe pipeline.

Usage

filter keeps the elements for which predicate returns a truthy value. A type predicate narrows the element type of the result. It is lazy-capable: inside a pipe it is fused with adjacent lazy operations.

import { filter, pipe } from 'es-toolkit/fp';

// Keep even numbers.
pipe(
  [1, 2, 3, 4],
  filter(x => x % 2 === 0)
); // => [2, 4]

// The index is available as the second argument.
pipe(
  [10, 20, 30, 40],
  filter((_value, index) => index % 2 === 0)
); // => [10, 30]

A type guard narrows the result type.

import { filter, pipe } from 'es-toolkit/fp';

const result = pipe(
  [1, 'a', 2, 'b'],
  filter((x): x is string => typeof x === 'string')
);
// result is typed as string[] and equals ['a', 'b']

Parameters

predicate(value: T, index: number) => booleanrequired

A function called for each element; return true to keep the element. A type guard (value is S) narrows the result.

Returns

A function that maps a readonly T[] to a filtered array. With a type guard, the result is S[].

(array: readonly T[]) => T[]
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.