2020-08-04 15:13:35 +08:00
---
id: 5cfa550e84205a357704ccb6
2021-02-06 04:42:36 +00:00
title: Use Destructuring Assignment to Extract Values from Objects
2020-08-04 15:13:35 +08:00
challengeType: 1
forumTopicId: 301216
2021-01-13 03:31:00 +01:00
dashedName: use-destructuring-assignment-to-extract-values-from-objects
2020-08-04 15:13:35 +08:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
< dfn > Destructuring assignment< / dfn > is special syntax introduced in ES6, for neatly assigning values taken directly from an object.
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
Consider the following ES5 code:
2020-08-04 15:13:35 +08:00
```js
const user = { name: 'John Doe', age: 34 };
const name = user.name; // name = 'John Doe'
const age = user.age; // age = 34
```
2021-02-06 04:42:36 +00:00
Here's an equivalent assignment statement using the ES6 destructuring syntax:
2020-08-04 15:13:35 +08:00
```js
const { name, age } = user;
// name = 'John Doe', age = 34
```
2021-02-06 04:42:36 +00:00
Here, the `name` and `age` variables will be created and assigned the values of their respective values from the `user` object. You can see how much cleaner this is.
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
You can extract as many or few values from the object as you want.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
# --instructions--
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
Replace the two assignments with an equivalent destructuring assignment. It should still assign the variables `today` and `tomorrow` the values of `today` and `tomorrow` from the `HIGH_TEMPERATURES` object.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
# --hints--
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
You should remove the ES5 assignment syntax.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
```js
assert(
2021-02-06 04:42:36 +00:00
!__helpers
.removeJSComments(code)
.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
2020-12-16 00:37:30 -07:00
);
2020-08-04 15:13:35 +08:00
```
2021-02-06 04:42:36 +00:00
You should use destructuring to create the `today` variable.
2020-08-04 15:13:35 +08:00
```js
2020-12-16 00:37:30 -07:00
assert(
2021-02-06 04:42:36 +00:00
__helpers
.removeJSComments(code)
.match(
/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
2020-12-16 00:37:30 -07:00
);
```
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
You should use destructuring to create the `tomorrow` variable.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
```js
assert(
2021-02-06 04:42:36 +00:00
__helpers
.removeJSComments(code)
.match(
/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
2020-12-16 00:37:30 -07:00
);
2020-08-04 15:13:35 +08:00
```
2021-02-06 04:42:36 +00:00
`today` should be equal to `77` and `tomorrow` should be equal to `80` .
```js
assert(today === 77 & & tomorrow === 80);
```
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
// Only change code below this line
const today = HIGH_TEMPERATURES.today;
const tomorrow = HIGH_TEMPERATURES.tomorrow;
// Only change code above this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
const { today, tomorrow } = HIGH_TEMPERATURES;
```