performance from by - Javascript: Remove attribute for all objects in array

3 Answers

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

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

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?

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;

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