Skip to content

Instantly share code, notes, and snippets.

@mmontes11
Last active February 3, 2020 14:52
Show Gist options
  • Save mmontes11/d6fc7bac96e8b405666a72776c4c55d3 to your computer and use it in GitHub Desktop.
Save mmontes11/d6fc7bac96e8b405666a72776c4c55d3 to your computer and use it in GitHub Desktop.
Index an array by a single fields
const indexByField = (array, field) =>
array.reduce((acc, it) => {
const key = it[field];
const value = acc[key] ? [...acc[key], it] : [it];
return {
...acc,
[key]: value,
};
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment