Skip to content

Instantly share code, notes, and snippets.

@nblackburn
Last active December 7, 2024 21:08
Show Gist options
  • Select an option

  • Save nblackburn/138fc48908c20fef64b436b4c04d5c1d to your computer and use it in GitHub Desktop.

Select an option

Save nblackburn/138fc48908c20fef64b436b4c04d5c1d to your computer and use it in GitHub Desktop.

Revisions

  1. nblackburn revised this gist Jul 24, 2024. 2 changed files with 12 additions and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,5 @@ const data = [
    ];

    groupBy(data, (d) => d.id); // { a: [ { id: 'a' }, { id: 'a' } ], b: [ { id: 'b' } ] }
    groupByArray(data, (d) => d.id); // [ { key: 'a', items: [{ id: 'a' }, { id: 'a' }] }, { key: 'b', items: [{ id: 'b' }] } ]
    ```
    11 changes: 11 additions & 0 deletions groupBy.js
    Original file line number Diff line number Diff line change
    @@ -16,4 +16,15 @@ const groupBy = (items, callback) => {
    });

    return groups;
    };

    const groupByArray = (items, callback) => {
    const groups = groupBy(items, callback);

    return Object.keys(groups).map((group) => {
    return {
    key: group,
    items: groups[group]
    };
    });
    };
  2. nblackburn revised this gist May 14, 2024. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions groupBy.js
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,16 @@ const groupBy = (items, callback) => {

    items.forEach((item) => {
    const key = callback(item) ?? 'ungrouped';

    const keys = Array.isArray(key) ? key : [key];

    // Create the group if it doesn't exist.
    if (!groups[key]) {
    groups[key] = [];
    }
    keys.forEach((key) => {
    if (!groups[key]) {
    groups[key] = [];
    }

    groups[key].push(item);
    groups[key].push(item);
    });
    });

    return groups;
  3. nblackburn revised this gist May 14, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion groupBy.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ const groupBy = (items, callback) => {
    const groups = {};

    items.forEach((item) => {
    const key = callback(item);
    const key = callback(item) ?? 'ungrouped';

    // Create the group if it doesn't exist.
    if (!groups[key]) {
  4. nblackburn created this gist May 14, 2024.
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    ## Group By

    Group items by the key returned in the callback.

    ```javascript
    const data = [
    { id: 'a' },
    { id: 'b' },
    { id: 'a' }
    ];

    groupBy(data, (d) => d.id); // { a: [ { id: 'a' }, { id: 'a' } ], b: [ { id: 'b' } ] }
    ```
    16 changes: 16 additions & 0 deletions groupBy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    const groupBy = (items, callback) => {
    const groups = {};

    items.forEach((item) => {
    const key = callback(item);

    // Create the group if it doesn't exist.
    if (!groups[key]) {
    groups[key] = [];
    }

    groups[key].push(item);
    });

    return groups;
    };