Skip to content

Instantly share code, notes, and snippets.

@thiagovilla
Created August 31, 2018 19:13
Show Gist options
  • Save thiagovilla/cf497901c8df99374805ccc9eb197101 to your computer and use it in GitHub Desktop.
Save thiagovilla/cf497901c8df99374805ccc9eb197101 to your computer and use it in GitHub Desktop.
Function that normalizes a collection (an array) of objects into an 'allIds' ordered array and a 'byId' object whose keys are each individual object id
const normalize = items => ({
byId: items.reduce((_items, item) => ({
..._items,
[item.id]: item
}), {}),
allIds: items.map(item => item.id)
})
export default normalize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment