Im trying to turn this:
[{price: 30, date: 05}, {price: 60, date: 05}, {price: 40, date: 06}, {price: 70, date: 06} ]
into this:
[{price: 90, date: 05}, {price 110, date: 06}]
Does anyone know how to do this in typescript? I keep getting the following error for trying to type the initial value:
Types of parameters 'acc' and 'previousValue' are incompatible.
Here’s what I tried:
const reducedExpenses = expenses.reduce(
(acc, { date, price }, index) => {
if (acc[index].date === date) {
return { ...acc[index], price: (acc[index].price += price) };
}
return acc.push({
date,
price,
});
},
[{ date: -1, price: 0 }]
);
2
Answers
Your issue is that Array.prototype.push() returns a number; the new length of the array. You’re using this value as the accumulator.
What you’re trying to do is most easily achieved by creating a map of keys-to-sums then transforming that map back into an array.
I’ve used a Map here instead of a plain object in order to preserve your (rather odd) octal literal
date
properties without losing fidelity to string serialisation.You should always return an array, instead of returning an object in one branch and a number in another.
To simplify the code, you can start the accumulator as an empty array (giving
acc
an explicit type) and use.at(-1)
to access the previous value.