@jitesoft/group-by

Makes all entries of an array - which returns a given key from callback - grouped under its key in resulting object.

Usage no npm install needed!

<script type="module">
  import jitesoftGroupBy from 'https://cdn.skypack.dev/@jitesoft/group-by';
</script>

README

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