2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 56bbb991ad1ed5201cd392d3
|
|
|
|
title: Delete Properties from a JavaScript Object
|
|
|
|
challengeType: 1
|
2019-02-14 12:24:02 -05:00
|
|
|
videoUrl: 'https://scrimba.com/c/cDqKdTv'
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 17560
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
We can also delete properties from objects like this:
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
|
|
`delete ourDog.bark;`
|
2020-03-25 08:07:13 -07:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ourDog = {
|
|
|
|
"name": "Camper",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["everything!"],
|
|
|
|
"bark": "bow-wow"
|
|
|
|
};
|
|
|
|
|
|
|
|
delete ourDog.bark;
|
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
After the last line shown above, `ourDog` looks like:
|
2020-03-25 08:07:13 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
{
|
|
|
|
"name": "Camper",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["everything!"]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
Delete the `"tails"` property from `myDog`. You may use either dot or bracket notation.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
You should delete the property `"tails"` from `myDog`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof myDog === 'object' && myDog.tails === undefined);
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
You should not modify the `myDog` setup.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(code.match(/"tails": 1/g).length > 0);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
(function(z){return z;})(myDog);
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --seed-contents--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
|
|
|
var myDog = {
|
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"],
|
|
|
|
"bark": "woof"
|
|
|
|
};
|
|
|
|
|
2020-03-02 23:18:30 -08:00
|
|
|
// Only change code below this line
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
var myDog = {
|
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"],
|
|
|
|
"bark": "woof"
|
|
|
|
};
|
|
|
|
delete myDog.tails;
|
|
|
|
```
|