We can use destructuring to remove a property from all objects in an array.
For instance, we can write:
const array = [{
"bad": "something",
"good": "something"
}, {
"bad": "something",
"good": "something"
}];
const newArray = array.map(({
bad,
...keepAttrs
}) => keepAttrs)
console.log(newArray)
We want to remove the bad
property from each object in array
.
To do that, we call array.map
with a callback that destructures the object by separating the bad
property from the rest of the properties, which are kept in the keepAttrs
object.
We return that so we get that as the value.
And so newArray
is:
[
{
"good": "something"
},
{
"good": "something"
}
]