I have the following object:
var myArr = {one:'1',two:'2',three:'3',four:'4',five:'5'};
I want to delete three properties from that object at once like:
delete myArr[one, three, five];
and it is failing. Do I have to perform an independent delete for each property like so:
delete myArr[one]; delete myArr[three]; delete myArr[five];
Thank you.
You could use an array for the keys and iterate for deleting.
var object = { one: '1', two: '2', three: '3', four: '4', five: '5' };
['one', 'three', 'five'].forEach(function (k) {
delete object[k];
});
console.log(object);
With 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With