Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jarvis1010/3a2ea785026e09b9d0fd363d0468260e to your computer and use it in GitHub Desktop.
Save Jarvis1010/3a2ea785026e09b9d0fd363d0468260e to your computer and use it in GitHub Desktop.
const collection = [
{ id: 1, name: “Han Solo”, title: “Scruffy Looking Nerf Herder”, allegiance: “rebellion” },
{ id: 2, name: “Darth Vader”, title: “Sith Lord”, allegiance: “empire” },
{ id: 3, name: “K-2SO”, title: “Sass-bot”, allegiance: “rebellion” },
{ id: 4, name: “Wilhuff Tarkin”, title: “Grand Moff”, allegiance: “empire” },
];
var sortedCollection = collection.sort(function(a,b){
return a.name.localeCompare(b.name);
});
var idsOnly=collection.map(function(item){
return item.id;
});
var mutatedCollection=collection.map(function(item){
item.name=item.name+" - deceased";
return item;
});
collection.shift();
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];
})
}
groupBy(collection,"allegiance")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment