Basic Data Structures: Use Delete Keyword to Remove Object Properties

Hey guys. I ended up getting the right answer but one of my previous answers half-worked an I am unsure why. I typed in one line of code delete foods['oranges', 'plums', 'strawberries'];
I passed the removal test but it said I failed the "The foods object only has three keys: apples , grapes , and bananas" test. I then redid the problem by typing the delete keyword referring to each property of the object and it worked. So my question is why does it still fail the test? Are the objects left behind even though the properties are deleted?

You can’t (afaik) delete all three properties at the same time. You’ll need three lines - one for each property.

And rather than use the bracket notation, the dot notation would be simpler, like in the example:

delete foods.apples;

Basically you’ve got to do that for each of the properties you want removed.

Thanks! I tried dot notation as well and it worked fine. It was just strange because it said the properties were successfully removed but it said it failed at testing for the other three properties being the only one left.

Yes, that test is probably just looking to see if the delete keyword is present. The tests don’t always explain themselves as well as they should.

But just to be clear, the bracket notation wasn’t an issue. But we typically use dot notation. The only time you need bracket notation is if the property is stored in a variable or if the property name is not a valid JS name. The issue with your code was that you were trying to delete three things at once.

1 Like

Alright I get it now. I got to keep that in mind when working with future tests on here. Thanks for all of the help. It definitely makes sense to only bring out the brackets when referencing a variable.