Created
August 21, 2014 12:24
-
-
Save renestalder/6ecd272193dbbae1c429 to your computer and use it in GitHub Desktop.
Group Array Items
This file contains hidden or 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
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]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example show, grouping items of an array be lastname and age properties.