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