https://github.com/jitesoft/js-group-by
  
  
     
    https://github.com/jitesoft/js-group-by
  
function group-by grouping hacktoberfest javascript
        Last synced: 2 months ago 
        JSON representation
    
- Host: GitHub
- URL: https://github.com/jitesoft/js-group-by
- Owner: jitesoft
- License: mit
- Created: 2019-11-03T16:04:56.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-24T15:14:18.000Z (over 1 year ago)
- Last Synced: 2025-01-04T10:08:51.318Z (10 months ago)
- Topics: function, group-by, grouping, hacktoberfest, javascript
- Language: JavaScript
- Homepage:
- Size: 2.89 MB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
- 
            Metadata Files:
            - Readme: README.md
- License: LICENSE
 
Awesome Lists containing this project
README
          # Group By
[](https://www.npmjs.com/package/@jitesoft/group-by)
[](https://dev.snyk.io/test/npm/@jitesoft/group-by)
[](https://gitlab.com/jitesoft/open-source/javascript/group-by/commits/master)
[](https://gitlab.com/jitesoft/open-source/javascript/group-by-by/commits/master)
[](https://www.npmjs.com/package/@jitesoft/group-by)
[](https://opencollective.com/jitesoft-open-source)
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:
```js
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" }
       ]
};
```