2018-10-10 18:03:03 -04:00
---
id: 587d7b89367417b2b2512b49
2021-02-06 04:42:36 +00:00
title: Use Destructuring Assignment to Assign Variables from Objects
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:13:35 +08:00
forumTopicId: 301215
2021-01-13 03:31:00 +01:00
dashedName: use-destructuring-assignment-to-assign-variables-from-objects
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Destructuring allows you to assign a new variable name when extracting values. You can do this by putting the new name after a colon when assigning the value.
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
Using the same object from the last example:
2020-08-04 15:13:35 +08:00
```js
const user = { name: 'John Doe', age: 34 };
```
2021-02-06 04:42:36 +00:00
Here's how you can give new variable names in the assignment:
2020-08-04 15:13:35 +08:00
```js
const { name: userName, age: userAge } = user;
// userName = 'John Doe', userAge = 34
```
2021-02-06 04:42:36 +00:00
You may read it as "get the value of `user.name` and assign it to a new variable named `userName` " and so on.
2020-12-16 00:37:30 -07:00
# --instructions--
2021-02-06 04:42:36 +00:00
Replace the two assignments with an equivalent destructuring assignment. It should still assign the variables `highToday` and `highTomorrow` the values of `today` and `tomorrow` from the `HIGH_TEMPERATURES` object.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should remove the ES5 assignment syntax.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
!code.match(/highToday = HIGH_TEMPERATURES\.today/g) &&
!code.match(/highTomorrow = HIGH_TEMPERATURES\.tomorrow/g)
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should use destructuring to create the `highToday` variable.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
code.match(
2021-02-06 04:42:36 +00:00
/(var|const|let)\s*{\s*(today\s*:\s*highToday[^}]*|[^,]*,\s*today\s*:\s*highToday\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 `highTomorrow` variable.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
```js
assert(
code.match(
2021-02-06 04:42:36 +00:00
/(var|const|let)\s*{\s*(tomorrow\s*:\s*highTomorrow[^}]*|[^,]*,\s*tomorrow\s*:\s*highTomorrow\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
2020-12-16 00:37:30 -07:00
)
);
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
`highToday` should be equal to `77` and `highTomorrow` should be equal to `80` .
```js
assert(highToday === 77 & & highTomorrow === 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
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
const highToday = HIGH_TEMPERATURES.today;
const highTomorrow = 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: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES;
```