2024-01-25 16:32:45 +00:00
|
|
|
export function groupBy<T, K extends keyof any>(
|
|
|
|
arr: T[],
|
|
|
|
key: (i: T) => K
|
|
|
|
): Record<K, T[]> {
|
2023-12-31 06:57:10 +00:00
|
|
|
return arr.reduce(
|
|
|
|
(groups, item) => {
|
2024-07-10 04:05:54 +00:00
|
|
|
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
|
2024-01-25 16:32:45 +00:00
|
|
|
;(groups[key(item)] ||= []).push(item)
|
|
|
|
return groups
|
2023-12-31 06:57:10 +00:00
|
|
|
},
|
2024-01-25 16:32:45 +00:00
|
|
|
{} as Record<K, T[]>
|
|
|
|
)
|
2023-12-31 06:57:10 +00:00
|
|
|
}
|