2018-09-30 23:01:58 +01:00
---
id: 587d7b89367417b2b2512b49
title: Use Destructuring Assignment to Assign Variables from Objects
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301215
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
2019-06-20 09:28:46 -05: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.
2019-05-17 06:20:30 -07:00
2019-06-07 10:19:03 -05:00
Using the same object from the last example:
2019-05-17 06:20:30 -07:00
```js
2019-06-07 10:19:03 -05:00
const user = { name: 'John Doe', age: 34 };
2019-05-17 06:20:30 -07:00
```
2019-06-07 10:19:03 -05:00
Here's how you can give new variable names in the assignment:
2019-05-17 06:20:30 -07:00
```js
2019-06-07 10:19:03 -05:00
const { name: userName, age: userAge } = user;
// userName = 'John Doe', userAge = 34
2019-05-17 06:20:30 -07:00
```
2019-06-26 15:23:32 -05:00
You may read it as "get the value of <code>user.name</code> and assign it to a new variable named <code>userName</code>" and so on.
2018-09-30 23:01:58 +01:00
</section>
## Instructions
<section id='instructions'>
2019-06-07 10:19:03 -05:00
Replace the two assignments with an equivalent destructuring assignment. It should still assign the variables <code>highToday</code> and <code>highTomorrow</code> the values of <code>today</code> and <code>tomorrow</code> from the <code>HIGH_TEMPERATURES</code> object.
2018-09-30 23:01:58 +01:00
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-06-07 10:19:03 -05:00
- text: You should remove the ES5 assignment syntax.
testString: assert(!code.match(/highToday = HIGH_TEMPERATURES\.today/g) && !code.match(/highTomorrow = HIGH_TEMPERATURES\.tomorrow/g))
- text: You should use destructuring to create the <code>highToday</code> variable.
2019-06-20 08:58:41 -05:00
testString: assert(code.match(/(var|const|let)\s*{\s*(today:\s*highToday[^}]*|[^,]*,\s*today\s*:\s*highToday\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g));
2019-06-07 10:19:03 -05:00
- text: You should use destructuring to create the <code>highTomorrow</code> variable.
2019-06-20 08:58:41 -05:00
testString: assert(code.match(/(var|const|let)\s*{\s*(tomorrow:\s*highTomorrow[^}]*|[^,]*,\s*tomorrow\s*:\s*highTomorrow\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g));
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
2019-06-07 10:19:03 -05:00
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
2018-09-30 23:01:58 +01:00
};
2019-06-07 10:19:03 -05:00
// change code below this line
const highToday = HIGH_TEMPERATURES.today;
const highTomorrow = HIGH_TEMPERATURES.tomorrow;
2018-09-30 23:01:58 +01:00
2019-06-07 10:19:03 -05:00
// change code above this line
console.log(yesterday) // should be not defined
console.log(highToday); // should be 77
console.log(highTomorrow); // should be 80
2018-09-30 23:01:58 +01:00
```
</div>
</section>
## Solution
<section id='solution'>
```js
2019-06-07 10:19:03 -05:00
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
2018-10-14 11:32:55 +05:30
};
2019-06-07 10:19:03 -05:00
// change code below this line
const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES;
// change code above this line
2018-10-14 11:32:55 +05:30
2019-06-07 10:19:03 -05:00
console.log(highToday); // should be 77
console.log(highTomorrow); // should be 80
2018-09-30 23:01:58 +01:00
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
</section>