Remove property for all objects in array
With ES6, you may deconstruct each object to create new one without named attributes:
const newArray = array.map(({dropAttr1, dropAttr2, ...keepAttrs}) => keepAttrs)
The only other ways are cosmetic and are in fact loops.
For example :
array.forEach(function(v){ delete v.bad });
Notes:
- if you want to be compatible with IE8, you'd need a shim for forEach. As you mention prototype, prototype.js also has a shim.
-
delete
is one of the worst "optimization killers". Using it often breaks the performances of your applications. You can't avoid it if you want to really remove a property but you often can either set the property toundefined
or just build new objects without the property.
I prefer to use map to delete the property and then return the new array item.
array.map(function(item) {
delete item.bad;
return item;
});