--- id: 587d7b7c367417b2b2512b1b challengeType: 1 forumTopicId: 301168 title: 使用 delete 关键字删除对象属性 --- ## Description
现在你已经知道什么是对象以及对象的基本特性和用途。总之,对象是以键值对的形式,灵活、直观地存储结构化数据的一种方式,并且查找对象属性的速度是很快的。在本章剩下的挑战中,我们会讲对象的几种常用操作,这样你能更好地在你的程序中使用这种有用的数据结构。 在之前的挑战中,我们已经试过新增和修改对象中的键值对。现在我们来看如何从一个对象中移除一个键值对。 我们再来看上一个挑战中的foods对象。如果我们想移除apples属性,我们可以使用delete关键字: ```js delete foods.apples; ```
## Instructions
请你用 delete 关键字来移除foods中的orangesplumsstrawberries属性。
## Tests
```yml tests: - text: foods对象应该只含有 3 个键:applesgrapesbananas。 testString: 'assert(!foods.hasOwnProperty(''oranges'') && !foods.hasOwnProperty(''plums'') && !foods.hasOwnProperty(''strawberries'') && Object.keys(foods).length === 3);' - text: 你应该用delete关键字来移除orangesplumsstrawberries属性。 testString: assert(code.search(/oranges:/) !== -1 && code.search(/plums:/) !== -1 && code.search(/strawberries:/) !== -1); ```
## Challenge Seed
```js let foods = { apples: 25, oranges: 32, plums: 28, bananas: 13, grapes: 35, strawberries: 27 }; // change code below this line // change code above this line console.log(foods); ```
## Solution
```js // solution required let foods = { apples: 25, oranges: 32, plums: 28, bananas: 13, grapes: 35, strawberries: 27 }; delete foods.oranges; delete foods.plums; delete foods.strawberries; console.log(foods); ```