Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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"
}
]
}
*/
```