Categories
JavaScript Answers

How Remove Property for All Objects in a JavaScript Array?

Spread the love

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"
  }
]

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *