Skip to content

Instantly share code, notes, and snippets.

@mikaello
Forked from JamieMason/group-objects-by-property.md
Last active October 26, 2025 18:51
Show Gist options
  • Save mikaello/06a76bca33e5d79cdd80c162d7774e9c to your computer and use it in GitHub Desktop.
Save mikaello/06a76bca33e5d79cdd80c162d7774e9c to your computer and use it in GitHub Desktop.

Revisions

  1. mikaello revised this gist Apr 28, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions group-objects-by-property.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,7 @@ const groupBy = keys => array =>

    <details>
    <summary>Click to see TypeScript version</summary>

    ```typescript
    /**
    * Group array of objects by given keys
    @@ -30,6 +31,7 @@ const groupBy = keys => array =>
    return objectsByKeyValue;
    }, {} as Record<string, T[]>);
    ```

    </details>

    ## Usage
  2. mikaello revised this gist Apr 28, 2021. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion group-objects-by-property.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Group Array of JavaScript Objects by Keys or Property Values
    # Group array of JavaScript objects by keys

    This fork of [JamieMason's implementation](https://gist.github.com/JamieMason/0566f8412af9fe6a1d470aa1e089a752) changes the `key` parameter to be an array of keys instead of just a single key. This makes it possible to group by multiple properties instead of just one.

    @@ -13,6 +13,25 @@ const groupBy = keys => array =>
    }, {});
    ```

    <details>
    <summary>Click to see TypeScript version</summary>
    ```typescript
    /**
    * Group array of objects by given keys
    * @param keys keys to be grouped by
    * @param array objects to be grouped
    * @returns an object with objects in `array` grouped by `keys`
    * @see <https://gist.github.com/mikaello/06a76bca33e5d79cdd80c162d7774e9c>
    */
    const groupBy = <T>(keys: (keyof T)[]) => (array: T[]): Record<string, T[]> =>
    array.reduce((objectsByKeyValue, obj) => {
    const value = keys.map((key) => obj[key]).join('-');
    objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
    return objectsByKeyValue;
    }, {} as Record<string, T[]>);
    ```
    </details>

    ## Usage

    ```js
  3. mikaello revised this gist Oct 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion group-objects-by-property.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Group Array of JavaScript Objects by Keys or Property Values

    This fork of [JamieMason's implementation](https://gist.github.com/JamieMason/0566f8412af9fe6a1d470aa1e089a752) 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.
    This fork of [JamieMason's implementation](https://gist.github.com/JamieMason/0566f8412af9fe6a1d470aa1e089a752) changes the `key` parameter to be an array of keys instead of just a single key. This makes it possible to group by multiple properties instead of just one.

    ## Implementation

  4. mikaello revised this gist Jun 6, 2019. 1 changed file with 67 additions and 31 deletions.
    98 changes: 67 additions & 31 deletions group-objects-by-property.md
    Original file line number Diff line number Diff line change
    @@ -1,47 +1,38 @@
    # 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](https://gist.github.com/JamieMason/0566f8412af9fe6a1d470aa1e089a752) 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

    ```js
    const groupBy = key => array =>
    const groupBy = keys => array =>
    array.reduce((objectsByKeyValue, obj) => {
    const value = obj[key];
    const value = keys.map(key => obj[key]).join('-');
    objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
    return objectsByKeyValue;
    }, {});
    ```

    Or using an implicit return (slower):

    ```js
    const groupBy = key => array =>
    array.reduce(
    (objectsByKeyValue, obj) => ({
    ...objectsByKeyValue,
    [obj[key]]: (objectsByKeyValue[obj[key]] || []).concat(obj)
    }),
    {}
    );
    ```

    ## Usage

    ```js
    const cars = [
    { brand: 'Audi', color: 'black' },
    { brand: 'Audi', color: 'white' },
    { brand: 'Ferarri', color: 'red' },
    { brand: 'Ford', color: 'white' },
    { brand: 'Peugot', color: 'white' }
    { brand: 'Audi', produced: '2016', color: 'black' },
    { brand: 'Audi', produced: '2017', color: 'white' },
    { brand: 'Ford', produced: '2016', color: 'red' },
    { brand: 'Ford', produced: '2016', color: 'white' },
    { brand: 'Peugot', produced: '2018', color: 'white' }
    ];

    const groupByBrand = groupBy('brand');
    const groupByColor = groupBy('color');
    const groupByBrand = groupBy(['brand']);
    const groupByColor = groupBy(['color']);
    const groupByBrandAndYear = groupBy(['brand', 'produced']);

    console.log(
    JSON.stringify({
    carsByBrand: groupByBrand(cars),
    carsByColor: groupByColor(cars)
    carsByColor: groupByColor(cars),
    carsByBrandAndYear: groupByBrandAndYear(cars)
    }, null, 2)
    );
    ```
    @@ -54,28 +45,31 @@ console.log(
    "Audi": [
    {
    "brand": "Audi",
    "produced": "2016",
    "color": "black"
    },
    {
    "brand": "Audi",
    "produced": "2017",
    "color": "white"
    }
    ],
    "Ferarri": [
    "Ford": [
    {
    "brand": "Ferarri",
    "brand": "Ford",
    "produced": "2016",
    "color": "red"
    }
    ],
    "Ford": [
    },
    {
    "brand": "Ford",
    "produced": "2016",
    "color": "white"
    }
    ],
    "Peugot": [
    {
    "brand": "Peugot",
    "produced": "2018",
    "color": "white"
    }
    ]
    @@ -84,29 +78,71 @@ console.log(
    "black": [
    {
    "brand": "Audi",
    "produced": "2016",
    "color": "black"
    }
    ],
    "white": [
    {
    "brand": "Audi",
    "produced": "2017",
    "color": "white"
    },
    {
    "brand": "Ford",
    "produced": "2016",
    "color": "white"
    },
    {
    "brand": "Peugot",
    "produced": "2018",
    "color": "white"
    }
    ],
    "red": [
    {
    "brand": "Ferarri",
    "brand": "Ford",
    "produced": "2016",
    "color": "red"
    }
    ]
    },
    "carsByBrandAndYear": {
    "Audi-2016": [
    {
    "brand": "Audi",
    "produced": "2016",
    "color": "black"
    }
    ],
    "Audi-2017": [
    {
    "brand": "Audi",
    "produced": "2017",
    "color": "white"
    }
    ],
    "Ford-2016": [
    {
    "brand": "Ford",
    "produced": "2016",
    "color": "red"
    },
    {
    "brand": "Ford",
    "produced": "2016",
    "color": "white"
    }
    ],
    "Peugot-2018": [
    {
    "brand": "Peugot",
    "produced": "2018",
    "color": "white"
    }
    ]
    }
    }
    ```
    ```

    See [playcode.io](https://playcode.io/334315?tabs=console&script.js&output) for example.
  5. @JamieMason JamieMason created this gist Sep 14, 2018.
    112 changes: 112 additions & 0 deletions group-objects-by-property.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@
    # Group Array of JavaScript Objects by Key or Property Value

    ## Implementation

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

    Or using an implicit return (slower):

    ```js
    const groupBy = key => array =>
    array.reduce(
    (objectsByKeyValue, obj) => ({
    ...objectsByKeyValue,
    [obj[key]]: (objectsByKeyValue[obj[key]] || []).concat(obj)
    }),
    {}
    );
    ```

    ## Usage

    ```js
    const cars = [
    { brand: 'Audi', color: 'black' },
    { brand: 'Audi', color: 'white' },
    { brand: 'Ferarri', color: 'red' },
    { brand: 'Ford', color: 'white' },
    { brand: 'Peugot', color: 'white' }
    ];

    const groupByBrand = groupBy('brand');
    const groupByColor = groupBy('color');

    console.log(
    JSON.stringify({
    carsByBrand: groupByBrand(cars),
    carsByColor: groupByColor(cars)
    }, null, 2)
    );
    ```

    ### Output

    ```json
    {
    "carsByBrand": {
    "Audi": [
    {
    "brand": "Audi",
    "color": "black"
    },
    {
    "brand": "Audi",
    "color": "white"
    }
    ],
    "Ferarri": [
    {
    "brand": "Ferarri",
    "color": "red"
    }
    ],
    "Ford": [
    {
    "brand": "Ford",
    "color": "white"
    }
    ],
    "Peugot": [
    {
    "brand": "Peugot",
    "color": "white"
    }
    ]
    },
    "carsByColor": {
    "black": [
    {
    "brand": "Audi",
    "color": "black"
    }
    ],
    "white": [
    {
    "brand": "Audi",
    "color": "white"
    },
    {
    "brand": "Ford",
    "color": "white"
    },
    {
    "brand": "Peugot",
    "color": "white"
    }
    ],
    "red": [
    {
    "brand": "Ferarri",
    "color": "red"
    }
    ]
    }
    }
    ```