Skip to content

Instantly share code, notes, and snippets.

@geomago
Last active June 18, 2020 16:26
Show Gist options
  • Save geomago/ed51213658166888489301df323553a4 to your computer and use it in GitHub Desktop.
Save geomago/ed51213658166888489301df323553a4 to your computer and use it in GitHub Desktop.
group_by_2
let grouped = cars.reduce(
(groups, curr) => {
let key = curr.make + ',' + curr.colour;
if (groups[key]===undefined) {
groups[key] = {count:1, minPrice:curr.price, maxPrice: curr.price };
} else {
groups[key].count++;
groups[key].minPrice = Math.min(groups[key].minPrice, curr.price);
groups[key].maxPrice = Math.max(groups[key].maxPrice, curr.price);
}
return groups;
},
{}
);
// RESULT IS:
{
"Ferrari,rosso corsa": {
count: 2,
count: 336000,
count: 336000
},
"Ferrari,giallo modena": {
count: 2,
count: 262000,
count: 262000
},
....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment