Skip to content

Instantly share code, notes, and snippets.

View hannemariavister's full-sized avatar

Hanne Maria Vister hannemariavister

View GitHub Profile
@hannemariavister
hannemariavister / group-objects-by-property.md
Created June 6, 2019 13:04 — forked from mikaello/group-objects-by-property.md
Group Array of JavaScript Objects by Key or Property Value

Group Array of JavaScript Objects by Keys or Property Values

This fork of JamieMason's implementation changes the key parameter to be an array of keys instead just a single key. This makes it possible to group by multiple properties instead of just one.

Implementation

const groupBy = keys => array =>
  array.reduce((objectsByKeyValue, obj) => {
    const value = keys.map(key => obj[key]).join('-');