I'm trying to take an array of objects and, without mutating the original array, rename a specific key in each object.
I've created a new variable and assigned it to the result of using 'map' on my original array (to avoid mutation). Within the 'map' method, I've tried to deconstruct the 'keyToChange' parameter and spread the rest of the values to the 'newKey'.
const renameKeys = (array, keyToChange, newKey) => {
const spreadArray = array.map(({keyToChange: newKey, ...rest}) => ({newKey, ...rest}));
return spreadArray;
};
The above code returns
'newKey: [undefined]' followed by the original array of objects.
Any help would be greatly appreciated.