FMHYedit/.vitepress/utils.ts

10 lines
245 B
TypeScript
Raw Normal View History

2023-12-31 06:57:10 +00:00
export function groupBy<T, K extends keyof any>(arr: T[], key: (i: T) => K): Record<K, T[]> {
return arr.reduce(
(groups, item) => {
(groups[key(item)] ||= []).push(item);
return groups;
},
{} as Record<K, T[]>,
);
}