Home > Article > Web Front-end > How to Efficiently Remove Specific Properties from an Array of Objects in JavaScript?
Eliminating Properties from an Array of Objects
In JavaScript, you may encounter the need to remove certain properties from an array of objects. While you can iterate through each object and delete the desired property, this approach can be repetitive and tedious. Fortunately, there are more efficient ways to achieve this task.
One alternative is to leverage ES6's destructuring capabilities. By deconstructing each object, you can create a new object that excludes the unwanted properties. For instance, given the array:
const array = [{"bad": "something", "good":"something"},{"bad":"something", "good":"something"},...];
You can remove the "bad" property from each object using the following code:
const newArray = array.map(({bad, ...keepAttrs}) => keepAttrs)
Here, the map() method iterates over the array, and for each object, it creates a new object (keepAttrs) that includes all the properties except for "bad." The resulting newArray will contain objects without the "bad" property.
This approach provides a concise and efficient solution for removing properties from an array of objects, allowing you to avoid the need for explicit loops and property deletion.
The above is the detailed content of How to Efficiently Remove Specific Properties from an Array of Objects in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!