Identifies the base type of array elements
Identifies the narrowed type when using type guards
The array or array-like object to partition
Function to test each element. Return true to include in first array, false for second
OptionalthisArg: anyOptional value to use as this when executing callbackFn
A tuple of two arrays: [matching elements, non-matching elements]
arrPartition([1, 2, 3, 4, 5], x => x % 2 === 0); // [[2, 4], [1, 3, 5]]
arrPartition(["a", "bb", "ccc"], x => x.length > 1); // [["bb", "ccc"], ["a"]]
// With type guard
const mixed: (string | number)[] = [1, "a", 2, "b"];
const [strings, numbers] = arrPartition(mixed, (x): x is string => typeof x === "string");
// strings: string[], numbers: (string | number)[]
// Array-like objects
arrPartition({ length: 4, 0: 1, 1: 2, 2: 3, 3: 4 }, x => x > 2); // [[3, 4], [1, 2]]
The arrPartition() method splits an array into two arrays based on a predicate function. The first array contains elements for which the predicate returns true, the second contains the rest.