Created
December 25, 2017 20:26
-
-
Save arondius/e8872e1d392e7ccbeea7708482247b21 to your computer and use it in GitHub Desktop.
JS reducer
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
/* Popular Ice Cream Totals Quiz | |
* | |
* Using the data array and .reduce(): | |
* - Return an object where each property is the name of an ice cream flavor | |
* and each value is an integer that's the total count of that flavor | |
* - Store the returned data in a new iceCreamTotals variable | |
* | |
* Notes: | |
* - Do not delete the data variable | |
* - Do not alter any of the data content | |
*/ | |
const data = [ | |
{ name: 'Tyler', favoriteIceCreams: ['Strawberry', 'Vanilla', 'Chocolate', 'Cookies & Cream'] }, | |
{ name: 'Richard', favoriteIceCreams: ['Cookies & Cream', 'Mint Chocolate Chip', 'Chocolate', 'Vanilla'] }, | |
{ name: 'Amanda', favoriteIceCreams: ['Chocolate', 'Rocky Road', 'Pistachio', 'Banana'] }, | |
{ name: 'Andrew', favoriteIceCreams: ['Vanilla', 'Chocolate', 'Mint Chocolate Chip'] }, | |
{ name: 'David', favoriteIceCreams: ['Vanilla', 'French Vanilla', 'Vanilla Bean', 'Strawberry'] }, | |
{ name: 'Karl', favoriteIceCreams: ['Strawberry', 'Chocolate', 'Mint Chocolate Chip'] } | |
]; | |
iceCreamTotals = data.reduce((accumulator, currentValue) => { | |
currentValue.favoriteIceCreams.map((flavor) => { | |
if(flavor in accumulator) { | |
accumulator[flavor]++; | |
} else { | |
accumulator[flavor] = 1; | |
} | |
return accumulator; | |
} | |
) | |
return accumulator; | |
}, {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment