Skip to content

group

根据指定的键将数组元素分组到对象中。

基础用法

typescript
import { group } from 'radash'

const users = [
  { id: 1, name: 'Alice', age: 25, city: 'Beijing' },
  { id: 2, name: 'Bob', age: 30, city: 'Shanghai' },
  { id: 3, name: 'Charlie', age: 35, city: 'Beijing' },
  { id: 4, name: 'Diana', age: 28, city: 'Shanghai' }
]

const groupedByCity = group(users, user => user.city)
// {
//   'Beijing': [
//     { id: 1, name: 'Alice', age: 25, city: 'Beijing' },
//     { id: 3, name: 'Charlie', age: 35, city: 'Beijing' }
//   ],
//   'Shanghai': [
//     { id: 2, name: 'Bob', age: 30, city: 'Shanghai' },
//     { id: 4, name: 'Diana', age: 28, city: 'Shanghai' }
//   ]
// }

语法

typescript
function group<T, K extends string | number | symbol>(
  array: readonly T[],
  key: (item: T, index: number, array: readonly T[]) => K
): Record<K, T[]>

参数

  • array (readonly T[]): 要分组的数组
  • key (function): 用于生成分组键的函数
    • item (T): 当前元素
    • index (number): 当前元素的索引
    • array (readonly T[]): 原数组

返回值

返回一个对象,其中键是分组键,值是包含该分组所有元素的数组。

示例

按年龄分组

typescript
import { group } from 'radash'

const users = [
  { id: 1, name: 'Alice', age: 25 },
  { id: 2, name: 'Bob', age: 30 },
  { id: 3, name: 'Charlie', age: 25 },
  { id: 4, name: 'Diana', age: 30 },
  { id: 5, name: 'Eve', age: 35 }
]

const groupedByAge = group(users, user => user.age)
// {
//   25: [
//     { id: 1, name: 'Alice', age: 25 },
//     { id: 3, name: 'Charlie', age: 25 }
//   ],
//   30: [
//     { id: 2, name: 'Bob', age: 30 },
//     { id: 4, name: 'Diana', age: 30 }
//   ],
//   35: [
//     { id: 5, name: 'Eve', age: 35 }
//   ]
// }

按字符串长度分组

typescript
import { group } from 'radash'

const words = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']

const groupedByLength = group(words, word => word.length)
// {
//   5: ['apple'],
//   6: ['banana', 'cherry'],
//   4: ['date'],
//   10: ['elderberry'],
//   3: ['fig']
// }

按条件分组

typescript
import { group } from 'radash'

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

const groupedByParity = group(numbers, num => num % 2 === 0 ? 'even' : 'odd')
// {
//   even: [2, 4, 6, 8, 10],
//   odd: [1, 3, 5, 7, 9]
// }

按多个属性分组

typescript
import { group } from 'radash'

const products = [
  { id: 1, name: 'Laptop', category: 'Electronics', price: 999 },
  { id: 2, name: 'Phone', category: 'Electronics', price: 599 },
  { id: 3, name: 'Book', category: 'Books', price: 19 },
  { id: 4, name: 'Tablet', category: 'Electronics', price: 399 }
]

const groupedByCategory = group(products, product => product.category)
// {
//   'Electronics': [
//     { id: 1, name: 'Laptop', category: 'Electronics', price: 999 },
//     { id: 2, name: 'Phone', category: 'Electronics', price: 599 },
//     { id: 4, name: 'Tablet', category: 'Electronics', price: 399 }
//   ],
//   'Books': [
//     { id: 3, name: 'Book', category: 'Books', price: 19 }
//   ]
// }

使用索引分组

typescript
import { group } from 'radash'

const items = ['a', 'b', 'c', 'd', 'e']

const groupedByIndex = group(items, (_, index) => Math.floor(index / 2))
// {
//   0: ['a', 'b'],
//   1: ['c', 'd'],
//   2: ['e']
// }

注意事项

  1. 保持原数组不变: group 不会修改原数组,而是返回新的对象
  2. 键的唯一性: 相同键的元素会被收集到同一个数组中
  3. 空数组处理: 如果原数组为空,返回空对象
  4. 性能: 时间复杂度为 O(n),其中 n 是数组长度

与其他方法的区别

  • reduce(): 可以用于分组,但需要手动构建结果对象
  • group(): 专门用于分组,语法更简洁
  • groupBy(): 类似功能,但 group 更直观

实际应用场景

  1. 数据分析: 按类别、地区、时间等维度分组数据
  2. 用户管理: 按角色、部门、状态等分组用户
  3. 商品管理: 按类别、品牌、价格区间等分组商品
  4. 日志分析: 按时间、级别、来源等分组日志

Released under the MIT License.