refactor post model
This commit is contained in:
parent
62f9de9546
commit
30025b4044
8 changed files with 373 additions and 151 deletions
19
src/utils/groupByAndMap.ts
Normal file
19
src/utils/groupByAndMap.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue