partition
Splits an array into two groups based on a predicate function.
The first group contains elements for which the predicate returns true,
the second group contains the rest.
null and undefined are treated as empty arrays and return [[], []].
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | null | undefined | The array to partition |
predicate | function | Function that returns true for elements in the first group |
Returns
Section titled “Returns”[T[], T[]] — A tuple of two arrays: [matching, non-matching]
Examples
Section titled “Examples”Split numbers by parity
Section titled “Split numbers by parity”Splits an array into even and odd numbers using a predicate.
Separate active and inactive users
Section titled “Separate active and inactive users”Partitions an array of objects based on a boolean property.
Handle empty array
Section titled “Handle empty array”Returns two empty arrays when the input is empty.
