Created
February 15, 2018 13:20
-
-
Save camarin24/c53054cc434cb00b9c047bc8f11393f5 to your computer and use it in GitHub Desktop.
Group By in Js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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