Delete multiple object properties

Can we delete several properties of object by using delete keyword?


let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};

// Only change code below this line
delete foods.oranges;
delete foods.plums;

delete foods.strawberries;


// Only change code above this line

console.log(foods);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36

Challenge: Use the delete Keyword to Remove Object Properties

Link to the challenge:

I mean not repeating in three lines//

Why not try it?

According to the docs:

The JavaScript delete operator removes a property from an object …

and

Where expression should evaluate to a property reference, e.g.:…

and

property

The property to delete.

So, it seems that the answer is no.

Of course, you could always write a function to accept the object reference and an array of property names.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.