Created
April 12, 2023 10:49
-
-
Save jkohlin/5246bc0a0758332e98dc89169d22140f to your computer and use it in GitHub Desktop.
groupByMulti
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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