Skip to content

Instantly share code, notes, and snippets.

@renestalder
Created August 21, 2014 12:24
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 renestalder/6ecd272193dbbae1c429 to your computer and use it in GitHub Desktop.
Save renestalder/6ecd272193dbbae1c429 to your computer and use it in GitHub Desktop.
Group Array Items
function 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];
})
}
var result = groupBy(list, function(item)
{
return [item.lastname, item.age];
});
@renestalder
Copy link
Author

Example show, grouping items of an array be lastname and age properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment