6

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.

8
  • 6
    correct me if I'am wrong but myArr is not an array!! Commented Nov 25, 2016 at 13:34
  • I have said that its an array object Commented Nov 25, 2016 at 13:35
  • 4
    it's an object. There's no such thing as an "array object" in JavaScript. And yes, the delete statement can delete only one property at a time. Commented Nov 25, 2016 at 13:35
  • 1
    the question should be reworded with Delete multiple properties using a single delete. Commented Nov 25, 2016 at 13:39
  • Thank Pointy. For the purposes of distinguishing the two types of arrays in Javascript, i use 'array object' for object literal var arr={} and 'array' for anything var arr=[]. Thank you again! Commented Nov 25, 2016 at 13:41

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

1 Comment

although i like a variation of this answer like: for (var i = 0, a= obj.length; i < a; i++) { delete obj[i]);}, i must declare you 'javascript offensive'. Sleek things this class comes from sleek minds. Thanks a million!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.