Skip to content

Instantly share code, notes, and snippets.

@camarin24
Created February 15, 2018 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camarin24/c53054cc434cb00b9c047bc8f11393f5 to your computer and use it in GitHub Desktop.
Save camarin24/c53054cc434cb00b9c047bc8f11393f5 to your computer and use it in GitHub Desktop.
Group By in Js
groupBy( array , f )
{
var groups = {};
array.forEach( function( o )
{
var group = JSON.stringify( f(o) );
groups[group] = groups[group] || [];
groups[group].push( o );
});
return Object.keys(groups).map( function( group )
{
return groups[group];
})
}
let f = this.groupBy(array,(item) => {
return [item.key];
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment