19 lines
358 B
TypeScript
19 lines
358 B
TypeScript
export function groupByAndMap<T, U>(
|
|
items: T[],
|
|
groupBy: (item: T) => string,
|
|
map: (item: T) => U,
|
|
): Record<string, U[]> {
|
|
const groupings: Record<string, U[]> = {}
|
|
|
|
for (const item of items) {
|
|
const key = groupBy(item)
|
|
|
|
if (!groupings[key]) {
|
|
groupings[key] = []
|
|
}
|
|
|
|
groupings[key].push(map(item))
|
|
}
|
|
|
|
return groupings
|
|
}
|