Skip to content

Instantly share code, notes, and snippets.

View alexandrzavalii's full-sized avatar
🏠
Working from home

alex alexandrzavalii

🏠
Working from home
View GitHub Profile
@alexandrzavalii
alexandrzavalii / group-objects-by-property.md
Created August 16, 2019 07:23 — forked from JamieMason/group-objects-by-property.md
Group Array of JavaScript Objects by Key or Property Value

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;