Last active
June 18, 2020 16:26
-
-
Save geomago/ed51213658166888489301df323553a4 to your computer and use it in GitHub Desktop.
group_by_2
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
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