Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/damianc/groupby
Method to group an array of objects.
https://github.com/damianc/groupby
Last synced: about 1 month ago
JSON representation
Method to group an array of objects.
- Host: GitHub
- URL: https://github.com/damianc/groupby
- Owner: damianc
- Created: 2024-03-03T20:00:23.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-03-03T20:29:16.000Z (10 months ago)
- Last Synced: 2024-03-03T21:24:02.055Z (10 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# groupBy
Method to group an array of objects.
```
groupBy(
key: string | (
item: Record,
index: number
) => string,
selector?: (
item: Record
) => any
)
```## Examples
```
const data = [
{lang: 'en', title: 'Coding Apes'},
{lang: 'pl', title: 'Kodujące małpy'},
{lang: 'en', title: 'Cracking JS'}
];// group titles by lang of book
console.log(
data.groupBy('lang', book => book.title)
);/*
{
"en": [
"Coding Apes",
"Cracking JS"
],
"pl": [
"Kodujące małpy"
]
}
*/
``````
// group books by first letter of title
console.log(
data.groupBy(book => book.title[0])
);/*
{
"C": [
{
"lang": "en",
"title": "Coding Apes"
},
{
"lang": "en",
"title": "Cracking JS"
}
],
"K": [
{
"lang": "pl",
"title": "Kodujące małpy"
}
]
}
*/
```