Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jitesoft/js-group-by
https://github.com/jitesoft/js-group-by
function group-by grouping hacktoberfest javascript
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/jitesoft/js-group-by
- Owner: jitesoft
- License: mit
- Created: 2019-11-03T16:04:56.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-24T15:14:18.000Z (8 months ago)
- Last Synced: 2024-05-03T19:49:53.517Z (6 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
[![npm (scoped)](https://img.shields.io/npm/v/@jitesoft/group-by)](https://www.npmjs.com/package/@jitesoft/group-by)
[![Known Vulnerabilities](https://dev.snyk.io/test/npm/@jitesoft/group-by/badge.svg)](https://dev.snyk.io/test/npm/@jitesoft/group-by)
[![pipeline status](https://gitlab.com/jitesoft/open-source/javascript/group-by/badges/master/pipeline.svg)](https://gitlab.com/jitesoft/open-source/javascript/group-by/commits/master)
[![coverage report](https://gitlab.com/jitesoft/open-source/javascript/group-by/badges/master/coverage.svg)](https://gitlab.com/jitesoft/open-source/javascript/group-by-by/commits/master)
[![npm](https://img.shields.io/npm/dt/@jitesoft/group-by)](https://www.npmjs.com/package/@jitesoft/group-by)
[![Back project](https://img.shields.io/badge/Open%20Collective-Tip%20the%20devs!-blue.svg)](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" }
]
};
```