Skip to content

partition

根据条件将数组分为两组,返回一个元组。

typescript
const [truthy, falsy] = partition(arr, isInTruthy);

参考

partition(arr, isInTruthy)

当您想根据特定条件将数组元素分为两组时,请使用 partition。将条件函数返回 true 的元素和返回 false 的元素分别放入不同的数组。

typescript
import { partition } from 'es-toolkit/array';

// 将数字数组分为偶数和奇数
const numbers = [1, 2, 3, 4, 5, 6];
const [evens, odds] = partition(numbers, x => x % 2 === 0);
// evens: [2, 4, 6]
// odds: [1, 3, 5]

// 根据特定条件分割对象数组
const users = [
  { name: 'Alice', active: true },
  { name: 'Bob', active: false },
  { name: 'Charlie', active: true },
];
const [activeUsers, inactiveUsers] = partition(users, user => user.active);
// activeUsers: [{ name: 'Alice', active: true }, { name: 'Charlie', active: true }]
// inactiveUsers: [{ name: 'Bob', active: false }]

对于空数组返回两个空数组。

typescript
import { partition } from 'es-toolkit/array';

const [truthy, falsy] = partition([], x => x > 0);
// truthy: []
// falsy: []

参数

  • arr (T[]): 要分为两组的数组。
  • isInTruthy ((value: T) => boolean): 决定每个元素应包含在第一个数组(truthy)还是第二个数组(falsy)中的条件函数。

返回值

([truthy: T[], falsy: T[]]): 由两个数组组成的元组。第一个数组包含条件为 true 的元素,第二个数组包含条件为 false 的元素。

采用 MIT 许可证发布。