我有以下对象:
var myArr = {one:'1',two:'2',three:'3',four:'4',five:'5'};
我想一次从该对象中删除三个属性:
delete myArr[one, three, five];
而且失败了。我是否必须为每个属性执行独立删除,如下所示:
delete myArr[one]; delete myArr[three]; delete myArr[five];
谢谢你。
最佳答案
您可以使用数组作为键,并迭代删除。
var object = { one: '1', two: '2', three: '3', four: '4', five: '5' };
['one', 'three', 'five'].forEach(function (k) {
delete object[k];
});
console.log(object);
与
Reflect.deleteProperty
var object = { one: '1', two: '2', three: '3', four: '4', five: '5' };
['one', 'three', 'five'].forEach(Reflect.deleteProperty.bind(null, object));
console.log(object);