Skip to content

Instantly share code, notes, and snippets.

@casesandberg
Created January 10, 2018 21:18
Show Gist options
  • Save casesandberg/f743ea67a8f2df71a0a0914f711bbd33 to your computer and use it in GitHub Desktop.
Save casesandberg/f743ea67a8f2df71a0a0914f711bbd33 to your computer and use it in GitHub Desktop.
// want to be able to ensure that the second param (key) exists as a key on the objects of the first type (array)
export const arrayToKeyedObj = (array: Array<{}>, key: string): {} => {
return array.reduce((acc, item) => {
acc[item[key]] = item
return acc
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment