Skip to content

Instantly share code, notes, and snippets.

@ryanvade
Created February 20, 2018 04:00
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 ryanvade/7177c8796420fa2b8bc6436426c5ceae to your computer and use it in GitHub Desktop.
Save ryanvade/7177c8796420fa2b8bc6436426c5ceae to your computer and use it in GitHub Desktop.
// given an array of teams
let teams = teamprovider.getTeams(); // don't remember the exact function name
// Create a Map
const map = new Map();
teams.forEach((team) => {
// get the 'key'
const key = team.name;
// get a collection by the 'key'
const collection = map.get(key);
// if the collection doesn't exist, create it
if(!collection) {
map.set(key, [team]);
}else {
// if the collection exists, add to the collection
collection.push(team);
}
});
// turn the map into an array of team 'groups' where the groups share the same team name
let teamGroupArr = Array.from(map);
// do stuff with the group array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment