5

I have a list

let list = [ 
{ 
  id: "247", 
  order_number: "21251", 
  tel: 13911111, 
  weight: "10kg" 
}, 
{ 
  id: "245", 
  order_number: "223", 
  tel: 31, 
  weight: "10kg" 
},
{ 
  id: "123", 
  order_number: "312312321", 
  tel: 3213123, 
  weight: "10kg" 
}
];

Now I only wan to remove the specific column, such as 'tel', to get a new list. Is there any elegant way to do it? or I have to loop the whole data to use splice method?

Ian
  • 354
  • 1
  • 5
  • 22
  • `findIndex` followed by `splice`, surely that's straightforward enough? – CertainPerformance Jun 12 '18 at 02:29
  • Possible duplicate of [How to remove properties from an object array?](https://stackoverflow.com/questions/37222885/how-to-remove-properties-from-an-object-array), though you should pick the highest voted answer, not the accepted one, or the comment underneath it — in your case: `list.map(({id, order_number, weight}) => ({id, order_number, weight}))`. – Sebastian Simon Jun 12 '18 at 02:38

5 Answers5

12

I would argue against using delete keyword because you would be mutating the list instead of making a new one, and also because of its behavior explained in the documentation, Specially those lines about:

  • Any property declared with let or const cannot be deleted from the scope within which they were defined
  • If a property with the same name exists on the object's prototype chain, then, after deletion, the object will use the property from the prototype chain (in other words, delete only has an effect on own properties).
  • Any property declared with var cannot be deleted from the global scope or from a function's scope.

Instead you can map().

listWithoutTel = list.map(({ tel, ...item }) => item);

Here you'd be using the rest parameters to put all properties but the unwanted one (in this case tel) of a destructured object in a variable named item and return in immediately.

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
2

In static way:

let list = [
  {
    id: "27",
    order_number: "21251",
    tel: 13911111,
    weight: "10kg"
  },
  {
    id: "245",
    order_number: "223",
    tel: 31,
    weight: "10kg"
  },
  {
    id: "123",
    order_number: "312312321",
    tel: 3213123,
    weight: "10kg"
  }
];
let new_list = list.map(function(obj) {
  return {
    id: obj.id,
    order_number: obj.order_number,
    weight: obj.weight
  }
});

console.log(list);
console.log(new_list)

This way, you keep both your old array and new array.

If you want to do it in dynamic way, you may use forEach to check the keys.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
tung yu
  • 94
  • 4
0

Use delete operator to remove the tel key from all objects in a loop

let list = [ 
{ 
  id: "247", 
  order_number: "21251", 
  tel: 13911111, 
  weight: "10kg" 
}, 
{ 
  id: "245", 
  order_number: "223", 
  tel: 31, 
  weight: "10kg" 
},
{ 
  id: "123", 
  order_number: "312312321", 
  tel: 3213123, 
  weight: "10kg" 
}
];
var list2 = JSON.parse(JSON.stringify(list));
for(i in list2) {
   delete list2[i].tel;
}

console.log(list2);
console.log(list);
Nandita Sharma
  • 13,287
  • 2
  • 22
  • 35
0

As far as I know you aren't going to find a way without looping. If you think about it, in order to delete the key from every object it will need to loop over it no matter what even if it's just a loop disguised as a simple function.

As far as simplicity goes I would recommend using:

list.forEach(function(x){delete x.tel});

let list = [ 
{ 
  id: "247", 
  order_number: "21251", 
  tel: 13911111, 
  weight: "10kg" 
}, 
{ 
  id: "245", 
  order_number: "223", 
  tel: 31, 
  weight: "10kg" 
},
{ 
  id: "123", 
  order_number: "312312321", 
  tel: 3213123, 
  weight: "10kg" 
}
];


list.forEach(function(x){ delete x.tel });

console.log(list);
Andrew
  • 531
  • 1
  • 6
  • 20
  • You could do this without looping: [Remove attribute for all objects in array](https://stackoverflow.com/q/18133635/4642212). You could even do this with some setup of a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). – Sebastian Simon Jun 12 '18 at 02:49
  • Correct me if I'm wrong, but in this case since every object has the key as opposed to a select few, wouldn't looping be just as efficient as another method because in both cases you will go to each index? – Andrew Jun 12 '18 at 02:53
  • You won’t iterate over each index, if you mutate [a single prototype](https://stackoverflow.com/a/18133757/4642212). You could also proxify the array or the objects and alter a few Proxy handlers to make the objects behave as if they didn’t have the `tel` property. Both approaches are `O(1)` rather than `O(n)`. – Sebastian Simon Jun 12 '18 at 02:56
-2

You can use delete keyword.

delete myObject.prop
Alexis Mateo
  • 57
  • 1
  • 7