tamda
Version:
Practical functional programming library for TypeScript
16 lines • 586 B
JavaScript
import { infer } from '../function/infer';
export function groupBy() {
return inferred.apply(undefined, arguments);
}
const inferred = infer((array, keyFn) => array.reduce((groups, item, index) => {
const key = `${keyFn(item, index, array)}`;
// Initializes the array if undefined for the key.
if (!groups[key]) {
groups[key] = [];
}
// Faster than spreading the object and array, safe here
// i.e. { ...groups, [key]: [...groups[key], item] }
groups[key].push(item);
return groups;
}, {}));
//# sourceMappingURL=groupBy.js.map