DEV Community

loading...

Discussion on: Reduce() Method in Javascript

Collapse
lexlohr profile image
Alex Lohr

Using modern syntax (arrow function, object decomposition) makes your example simpler and more readable:

const intake = meals.reduce(
  ({ carbs, fat, calories }, meal) => ({
    carbs: carbs + meal.carbs,
    fat: fat + meal.fat,
    calories: calories + meal.calories,
  }),
  {carbs: 0, fat: 0, calories: 0}
)
Enter fullscreen mode Exit fullscreen mode
Collapse
gamil91 profile image
Alexa Gamil Author

This is awesome! Thank you for this. Would it be ok if I update my blog and use that?

Collapse
lexlohr profile image
Alex Lohr

Please do. You're welcome.