Skip to content

Instantly share code, notes, and snippets.

@daraxdray
daraxdray / group-objects-by-property.md
Created Nov 18, 2019 — forked from JamieMason/group-objects-by-property.md
Group Array of JavaScript Objects by Key or Property Value
View group-objects-by-property.md

Group Array of JavaScript Objects by Key or Property Value

Implementation

const groupBy = key => array =>
  array.reduce((objectsByKeyValue, obj) => {
    const value = obj[key];
    objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
    return objectsByKeyValue;