A group-by function working with the built-in Array.reduce()
Prefer ES2024 Object.groupBy()
(supported since Node.js 21):
Object.groupBy(["Alpha", "Beta", "Animal"], (o) => o[0]);
//=> {A: ['Alpha', 'Animal'], B: ['Beta']}
npm install array-reduce-group-by
import {arrayReduceGroupBy} from "array-reduce-group-by";
["Alpha", "Beta", "Animal"]
.reduce(arrayReduceGroupBy((o) => o[0]), {});
//=> {A: ['Alpha', 'Animal'], B: ['Beta']}
Make sure not to forget the {}
in the end!
Type: (arrayElement) => string
Function to determine the key of a given array element