Here is a function I wrote:
type Data = {
x?: number[],
y?: number[],
z?: number[],
};
const sampleData = [{ x: 1, y: 2, z: 3 }, { x: 4, y: 7, z: 10 }];
const updatedData = sampleData.reduce((accumulator, element) => {
Object.entries(element!).forEach(([key, value]) => {
accumulator = { ...accumulator, [key]: [...(accumulator[key as keyof Data] || []), value] };
});
return accumulator;
}, {} as Data);
Although this code accomplishes its purpose, it triggers an eslint error stating no-param-reassign
.
I am aware that I can disable the rule, but I'm curious if there's a more optimal way to structure this reduce callback without reassigning accumulator
.