Javascript: Remove attribute for all objects in array

2 Answers

With ES6, you may deconstruct each object to create new one without named attributes:

const newArray ={dropAttr1, dropAttr2, ...keepAttrs}) => keepAttrs)

I want to remove the "bad" attribute from every object in the array. Is there a better way to do it than using a for loop and deleting it from every object?

var array = [{"bad": "something", "good":"something"},{"bad":"something", "good":"something"},...];

for (var i = 0, len = array.length; i < len; i++) {
    delete array[i].bad;

Just seems like there should be a way to use prototype, or something. I don't know. Ideas?

var array = [{"bad": "something", "good":"something"},{"bad":"something", "good":"something"}];
var results ={
  return {good : item["good"]}

A solution using prototypes is only possible when your objects are alike:

function Cons(g) { this.good = g; }
Cons.prototype.bad = "something common";
var array = [new Cons("something 1"), new Cons("something 2"), …];

But then it's simple (and O(1)):

delete Cons.prototype.bad;