最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove specific column from data array (JS) - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Jun 12, 2018 at 2:28 IanIan 3541 gold badge5 silver badges22 bronze badges 2
  • findIndex followed by splice, surely that's straightforward enough? – CertainPerformance 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})). – Sebastian Simon Commented Jun 12, 2018 at 2:38
Add a comment  | 

5 Answers 5

Reset to default 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.

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.

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);

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);

You can use delete keyword.

delete myObject.prop
发布评论

评论列表(0)

  1. 暂无评论