我有以下对象:
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];
谢谢。
答案 0 :(得分:10)
您可以使用数组作为键并迭代进行删除。
var object = { one: '1', two: '2', three: '3', four: '4', five: '5' };
['one', 'three', 'five'].forEach(function (k) {
delete object[k];
});
console.log(object);

var object = { one: '1', two: '2', three: '3', four: '4', five: '5' };
['one', 'three', 'five'].forEach(Reflect.deleteProperty.bind(null, object));
console.log(object);