Skip to content

Instantly share code, notes, and snippets.

Avatar
🙂
Working

Hussain Sherwani hussainahmad

🙂
Working
View GitHub Profile
@hussainahmad
hussainahmad / group-objects-by-property.md
Created Feb 9, 2022 — 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;