Last active
December 11, 2021 12:31
-
-
Save akhrszk/73454f91db50f12e568bbee3e318d218 to your computer and use it in GitHub Desktop.
convert Array to Map by key
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
const toMap = <T>(arr: T[], byId: (item: T) => string): Map<string, T> => { | |
const tuple = arr.reduce((acc, curr) => { | |
acc.push([byId(curr), curr]) | |
return acc | |
}, [] as [string, T][]) | |
return new Map(tuple) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment