2019-06-07 15:19:03 +00:00
---
id: 5cfa550e84205a357704ccb6
title: Use Destructuring Assignment to Extract Values from Objects
challengeType: 1
2019-08-05 16:17:33 +00:00
forumTopicId: 301216
2021-01-13 02:31:00 +00:00
dashedName: use-destructuring-assignment-to-extract-values-from-objects
2019-06-07 15:19:03 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2019-06-07 15:19:03 +00:00
< dfn > Destructuring assignment< / dfn > is special syntax introduced in ES6, for neatly assigning values taken directly from an object.
Consider the following ES5 code:
```js
const user = { name: 'John Doe', age: 34 };
2021-03-03 00:12:12 +00:00
const name = user.name;
const age = user.age;
2019-06-07 15:19:03 +00:00
```
2021-03-03 00:12:12 +00:00
`name` would have a value of the string `John Doe` , and `age` would have the number `34` .
2019-06-07 15:19:03 +00:00
Here's an equivalent assignment statement using the ES6 destructuring syntax:
```js
const { name, age } = user;
```
2021-03-03 00:12:12 +00:00
Again, `name` would have a value of the string `John Doe` , and `age` would have the number `34` .
2020-11-27 18:02:05 +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.
2019-06-07 15:19:03 +00:00
You can extract as many or few values from the object as you want.
2020-03-25 12:28:17 +00:00
2020-11-27 18:02:05 +00:00
# --instructions--
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.
# --hints--
You should remove the ES5 assignment syntax.
```js
assert(
2021-04-28 15:18:54 +00:00
!code.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
2020-11-27 18:02:05 +00:00
);
2019-06-07 15:19:03 +00:00
```
2020-11-27 18:02:05 +00:00
You should use destructuring to create the `today` variable.
2019-06-07 15:19:03 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(
2021-04-28 15:18:54 +00:00
code.match(/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)
2020-11-27 18:02:05 +00:00
);
```
You should use destructuring to create the `tomorrow` variable.
```js
assert(
2021-04-28 15:18:54 +00:00
code.match(/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)
2020-11-27 18:02:05 +00:00
);
```
`today` should be equal to `77` and `tomorrow` should be equal to `80` .
```js
assert(today === 77 & & tomorrow === 80);
```
# --seed--
## --seed-contents--
2019-06-07 15:19:03 +00:00
```js
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
2020-03-04 19:08:54 +00:00
// Only change code below this line
2019-06-07 15:19:03 +00:00
const today = HIGH_TEMPERATURES.today;
2019-07-19 09:04:45 +00:00
const tomorrow = HIGH_TEMPERATURES.tomorrow;
2019-06-07 15:19:03 +00:00
2020-03-04 19:08:54 +00:00
// Only change code above this line
2019-06-07 15:19:03 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2019-06-07 15:19:03 +00:00
```js
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
const { today, tomorrow } = HIGH_TEMPERATURES;
```