I have an array of objects, I want to remove a few properties of this object, by calling a function and passing the array and the key I want to delete.
I referred this answer on Stack Overflow and it works when I specify the key manually, like id
. It deletes the id
property of each object in the array.
const newArray = array.map(
({
id,
...otherAttributes
}) => otherAttributes
);
But when I pass the key to the function, and then use the key from the parameter, it is not able to detect the key in the object and so not able to delete it. The function body is something like this:
removeKeyFromObjectsArray(newArray, key) {
const newArray = products.map(
({
key,
...otherAttributes
}) => otherAttributes
);
return newArray;
I call this function using this.removeKeyFromObjectsArray(this.updatedProducts, 'id')
but the objects inside this array still has the key. (The this
is because it's inside a Vue App and I need to refer to a different function in the same instance).
This function works if we manually specify the key name like id
but it does not work when we pass the key string through a parameter and then use it, can someone explain what is the issue here and how can I solve it?