mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-15 18:30:39 +09:00
12 lines
296 B
TypeScript
12 lines
296 B
TypeScript
|
|
export function groupBy<T>(items: T[], keyFn: (item: T) => string): Record<string, T[]> {
|
||
|
|
const result: Record<string, T[]> = {};
|
||
|
|
for (const item of items) {
|
||
|
|
const key = keyFn(item);
|
||
|
|
if (!result[key]) {
|
||
|
|
result[key] = [];
|
||
|
|
}
|
||
|
|
result[key].push(item);
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|