Skip to content

Instantly share code, notes, and snippets.

@AitorAlejandro
Created November 25, 2020 17:43
Show Gist options
  • Save AitorAlejandro/174b0b2743f1da2d3b7cd0de2c5fe0a0 to your computer and use it in GitHub Desktop.
Save AitorAlejandro/174b0b2743f1da2d3b7cd0de2c5fe0a0 to your computer and use it in GitHub Desktop.
const indexBy = (key, xs) => xs.reduce(acc, el) => ({
...acc,
[el[key]]: el,
});
/*
example:
// const users = [
{id: 'id1', name: 'Aritz'},
{id: 'id2', name: 'Asier'},
]
const indexedUsers = indexBy('id', users);
{
id1: {id: 'id1', name: 'Aritz'}
id2: {id: 'id2', name: 'Asier'}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment