Use the delete Keyword to Remove Object Properties

Use the delete Keyword to Remove Object Properties
0.0 0

#1

Tell us what’s happening:

I know the correct answer but why does this not work?

Your code so far


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

// change code below this line
delete foods['oranges', 'plums', 'strawberries'];
// change code above this line

console.log(foods);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1 Safari/605.1.15.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/use-the-delete-keyword-to-remove-object-properties


#2

hi, I believe you cannot add multiple properties in bracket notation .You can access a single property at a time.

this would be valid:

delete foods['oranges'];
delete foods['plums'];
delete foods['strawberries'];

helpful link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

I hope this helps, Happy coding!


#3

By destructuring and using a map you could achieve what you want, check below code hope it helps!

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


['oranges','plums','strawberries'].map(x=> delete foods[x]);


console.log(foods);

#4

Although map does work, map is intended to return a new array. You can use forEach to accomplish the same thing without the extra overhead (memory usage) that map introduces.


#5

Although map has an overhead (and you are correct) it is much faster than forEach in terms of performance.


#6

Thanks for the clarification =]