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
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!
1 Like
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);
1 Like
Although map has an overhead (and you are correct) it is much faster than forEach in terms of performance.
2 Likes
Thanks for the clarification =]
1 Like
As RandellDawson said, using forEach to iterate over every value of your array is a more elegant practice in that case.
And it is really easy to understand when you read it
let items = ['oranges', 'plums', 'strawberries']
items.forEach(item=> delete foods[item])
1 Like