🧩 Creates an array of elements split into two groups: those that pass the predicate and those that don't.

Syntax

TypeScript
import { partition } from '@opentf/std';

partition<T>(arr: T[] = [], predicate: (val: T, index: number, arr: T[]) => boolean): [T[], T[]]

Parameters

  • arr: The source array.

  • predicate: The function invoked per element. Receives (value, index, array).

Returns

A tuple of [passing, failing] arrays.

Examples

TypeScript
partition([1, 2, 3, 4, 5], (n) => n % 2 === 0) //=> [[2, 4], [1, 3, 5]]

partition([0, 1, false, 2, '', 3], Boolean) //=> [[1, 2, 3], [0, false, '']]

partition([1, 2, 3], () => true) //=> [[1, 2, 3], []]

partition([1, 2, 3], () => false) //=> [[], [1, 2, 3]]
Last updated on
Edit this page