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']
// }
注意事項
- 保持原數組不變:
group
不會修改原數組,而是返回新的對象 - 鍵的唯一性: 相同鍵的元素會被收集到同一個數組中
- 空數組處理: 如果原數組為空,返回空對象
- 性能: 時間復雜度為 O(n),其中 n 是數組長度
與其他方法的區別
reduce()
: 可以用於分組,但需要手動構建結果對象group()
: 專門用於分組,語法更簡潔groupBy()
: 類似功能,但group
更直觀
實際應用場景
- 數據分析: 按類別、地區、時間等維度分組數據
- 用戶管理: 按角色、部門、狀態等分組用戶
- 商品管理: 按類別、品牌、價格區間等分組商品
- 日志分析: 按時間、級別、來源等分組日志