UNPKG

syncpack

Version:

Consistent dependency versions in large JavaScript Monorepos

12 lines (11 loc) 413 B
/** * Convert an array of objects to an object, where each property of the new * object is an array whose members share the same value for the given key. */ export function groupBy(key, array) { return array.reduce((objectsByKeyValue, obj) => { const value = obj[key]; objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj); return objectsByKeyValue; }, {}); }