Skip to content

Instantly share code, notes, and snippets.

@nikita-rudenko
Created May 6, 2020 07:55
Show Gist options
  • Select an option

  • Save nikita-rudenko/dad7bd14dd1a2328b2c859d0c392d8a9 to your computer and use it in GitHub Desktop.

Select an option

Save nikita-rudenko/dad7bd14dd1a2328b2c859d0c392d8a9 to your computer and use it in GitHub Desktop.
const cuisinesTemplate = {
american: [],
italian: [],
french: []
}
function serveBuffet(dishes) {
const buffet = dishes.reduce((acc, dish) => {
const cuisine = dish.cuisine.toLowerCase();
if (acc[cuisine]) acc[cuisine].push(dish);
else acc[cuisine] = [dish];
return acc;
}, {...cuisinesTemplate});
return buffet;
}
const dishes = [
{
cuisine: "American",
name: "Burger",
},
{
cuisine: "Italian",
name: "Pizza",
},
{
cuisine: "French",
name: "Baguette",
},
{
cuisine: "Italian",
name: "Spaghetti",
},
{
cuisine: "French",
name: "Ratatouille",
}
]
serveBuffet(dishes);
serveBuffet(dishes);
serveBuffet(dishes);
console.log(cuisinesTemplate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment