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

1.9.7 • 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" }
       ]
};

Dependents (1)

Package Sidebar

Install

npm i @jitesoft/group-by

Weekly Downloads

228

Version

1.9.7

License

MIT

Unpacked Size

50.6 kB

Total Files

7

Last publish

Collaborators

  • jitesoft~
  • johannestegner