Solución
solution.tsTypeScript
interface Item {
category: string;
value: number;
}
function sumByCategory(items: Item[]): Record<string, number> {
// Tu código aquí
return items.reduce(
(accumulativeValue, currentValue) => {
accumulativeValue[currentValue['category']] =
( accumulativeValue[currentValue['category']] !== undefined
? accumulativeValue[currentValue['category']]
: 0
) + currentValue['value'];
return accumulativeValue
},{}
);
}
// No modificar: necesario para evaluar el resultado.
export { sumByCategory };
0respuestas