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?

2
  • findIndex followed by splice, surely that's straightforward enough? Commented Jun 12, 2018 at 2:29
  • Possible duplicate of 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})). Commented Jun 12, 2018 at 2:38

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

if the property is like 'tel num' , how to handle it? @Christopher Francisco
Best answer I've seen. Cheers!
How could you make "tel" a variable?
@Ian I understand its pretty old post but did you get solution to your comment?
@Ian using developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… link from anwer post Its possible to pass feild name with space like listWithoutTel = list.map(({ 'tel num':telnum, ...item }) => item);
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.

1 Comment

Can this be made dynamic? User can select what columns to be removed (tel or tel and weight, ...)?
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);

2 Comments

The OP specified a new list. You have to first create a shallow copy of the array before deleting tel
Updated the answer. please check now @MaxK
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);

3 Comments

You could do this without looping: Remove attribute for all objects in array. You could even do this with some setup of a Proxy.
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?
You won’t iterate over each index, if you mutate a single prototype. 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).
-2

You can use delete keyword.

delete myObject.prop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.