Skip to content

damianc/groupBy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

groupBy

Method to group an array of objects.

groupBy(
  key: string | (
    item: Record<string,any>,
    index: number
  ) => string,
  selector?: (
    item: Record<string,any>
  ) => 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"
    }
  ]
}
*/

About

Method to group an array of objects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published