Skip to content

Instantly share code, notes, and snippets.

@jkohlin
Created April 12, 2023 10:49
Show Gist options
  • Save jkohlin/5246bc0a0758332e98dc89169d22140f to your computer and use it in GitHub Desktop.
Save jkohlin/5246bc0a0758332e98dc89169d22140f to your computer and use it in GitHub Desktop.
groupByMulti
type GroupByMulti = (list: any[], paths: { [key: string]: string }) => any
export const groupByMulti: GroupByMulti = (list, paths) => {
return (
list?.reduce((acc: { [key: string]: any }, row: any) => {
Object.keys(paths).forEach((key) => {
const path = paths[key]
if (getVal(row, path)) {
;(acc[key] = acc[key] || {})[getVal(row, path)] = (acc[key] = acc[key] || {})[getVal(row, path)] || []
acc[key][getVal(row, path)].push(row)
}
})
return acc
}, {}) || {}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment