Basic Data Structures - Use the delete Keyword to Remove Object Properties

Tell us what’s happening:
Describe your issue in detail here.

I have more of a question about this.

So the exercise has us deleting values in an object using delete foods.value;

So my question is this. Rather than continuously writing the delete keyword along with the expression, is it possible to just write it once using an array for the values?

Something like this…

delete foods.[value1, value2 etc…]???

Your code so far

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

// Only change code below this line

// Only change code above this line

console.log(foods);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge: Basic Data Structures - Use the delete Keyword to Remove Object Properties

Link to the challenge:

You cant just do delete foods[all the things i want to delete], but you could make an array with all the keys and loop over that array and use it to delete everything in that array like this:

let myArr = ['oranges', 'plums', 'strawberries'];

for(let i = 0; i < myArr.length; i++) {

delete foods[myArr[i]];

}
1 Like

Thank you very much for the reply. I see exactly what you did there and it makes sense. I was just wondering if JavaScript had some kinda shorter syntax to delete keys and values. Thank you again for your hasty reply.

1 Like

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