Medium
This question is a daily question and will count towards your daily streak.
What is the main advantage of using the reduce method when working with arrays in JavaScript?
1const expenses = [2 { category: 'food', amount: 15 },3 { category: 'transport', amount: 25 },4 { category: 'entertainment', amount: 40 },5];67const totalAmountByCategory = expenses.reduce((acc, expense) => {8 if (!acc[expense.category]) {9 acc[expense.category] = 0;10 }11 acc[expense.category] += expense.amount;12 return acc;13}, {});1415console.log(totalAmountByCategory);