如何使用单个删除从Object中删除多个属性?

时间:2016-11-25 13:32:40

标签: javascript arrays

我有以下对象:

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

谢谢。

1 个答案:

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




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