I have a function written like so:
type ChartData = {
x?: number[],
y?: number[],
z?: number[],
};
const data = [{ x: 1, y: 2, z: 3 }, { x: 4, y: 7, z: 10 }];
const formattedData = data.reduce((acc, el) => {
Object.entries(el!).forEach(([k, v]) => {
acc = { ...acc, [k]: [...(acc[k as keyof ChartData] || []), v] };
});
return acc;
}, {} as ChartData);
This code does what I want, but I'm getting an eslint error: no-param-reassign.
I know that I could disable the rule, but I wondered if there's a better way of writing this reduce callback so that I am not reassigning acc.