Skip to content

Function: partition()

ts
function partition<T>(array, predicate): [T[], T[]];

Splits an array into matched and unmatched groups based on a predicate. 根据断言函数将数组拆分为命中组和未命中组。

Type Parameters

Type ParameterDescription
TThe type of array items / 数组元素类型

Parameters

ParameterTypeDescription
arrayreadonly T[]The source array / 源数组
predicate(item, index, array) => booleanThe predicate used to split items / 用于拆分元素的断言函数

Returns

[T[], T[]]

A tuple of matched and unmatched groups / 命中组与未命中组的元组

Example

ts
partition([1, 2, 3, 4], (value) => value % 2 === 0)
// => [[2, 4], [1, 3]]

Since

1.2.0

Released under the MIT License.