I am trying to use the reduce method on an array of objects, with each object being a person and flavors of ice cream they like. The goal is to reduce this array into a single object that contains the flavors as properties and the total number of times the flavor is listed as the values.
the data:
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'] }
];
This is what I have so far, but I am honestly not sure on how to do it.
let iceCreamTotals = {
Strawberry: 0,
Vanilla: 0,
Chocolate: 0,
CookiesCream: 0,
MintChocolateChip: 0,
RockyRoad: 0,
Pistachio: 0,
Banana: 0,
FrenchVanilla: 0,
VanillaBean: 0
}
data.reduce((total, current) => {
return iceCreamTotals
}, 0)