Last active
February 3, 2020 14:52
-
-
Save mmontes11/d6fc7bac96e8b405666a72776c4c55d3 to your computer and use it in GitHub Desktop.
Index an array by a single fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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