Skip to content

Instantly share code, notes, and snippets.

@akhrszk
Last active December 11, 2021 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhrszk/73454f91db50f12e568bbee3e318d218 to your computer and use it in GitHub Desktop.
Save akhrszk/73454f91db50f12e568bbee3e318d218 to your computer and use it in GitHub Desktop.
convert Array to Map by key
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