Neolithic Prancing Minotaurs

    @jitesoft/group-by
    TypeScript icon, indicating that this package has built-in type declarations

    1.7.5 • Public • Published

    Group By

    npm (scoped) Known Vulnerabilities pipeline status coverage report npm Back project

    Simple method to group an array by a given key in the array. The method uses a callback function to choose the value to group the values by and returns an object which have all the ordered values in an array under the given key name.

    Example:

    import groupBy from '@jitesoft/group-by';
    
    let array = [ { "id": "abc", "value": "abc" },
                  { "id": "abc", "value": "def" },
                  { "id": "abc", "value": "ghi" },
                  { "id": "def", "value": "abc" },
                  { "id": "def", "value": "def" },
                  { "id": "ghi", "value": "abc" } ];
    
    let result = groupBy(array, (obj) => obj.id);
    
    // Output:
    let obj = {
           abc: [
             { "id": "abc", "value": "abc" },
             { "id": "abc", "value": "def" },
             { "id": "abc", "value": "ghi" }
           ],
           def: [
             { "id": "def", "value": "abc" },
             { "id": "def", "value": "def" }
           ],
           ghi: [
             { "id": "ghi", "value": "abc" }
           ]
    };

    Install

    npm i @jitesoft/group-by

    DownloadsWeekly Downloads

    81

    Version

    1.7.5

    License

    MIT

    Unpacked Size

    50.3 kB

    Total Files

    7

    Last publish

    Collaborators

    • jitesoft~
    • johannestegner