2018-10-10 18:03:03 -04:00
---
id: 587d7b89367417b2b2512b4a
2021-02-06 04:42:36 +00:00
title: Use Destructuring Assignment to Assign Variables from Nested Objects
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:13:35 +08:00
forumTopicId: 301214
2021-01-13 03:31:00 +01:00
dashedName: use-destructuring-assignment-to-assign-variables-from-nested-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
You can use the same principles from the previous two lessons to destructure values from nested objects.
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
Using an object similar to previous examples:
2020-08-04 15:13:35 +08:00
```js
const user = {
johnDoe: {
age: 34,
email: 'johnDoe@freeCodeCamp .com'
}
};
```
2021-02-06 04:42:36 +00:00
Here's how to extract the values of object properties and assign them to variables with the same name:
2020-08-04 15:13:35 +08:00
```js
const { johnDoe: { age, email }} = user;
```
2021-02-06 04:42:36 +00:00
And here's how you can assign an object properties' values to variables with different names:
2020-08-04 15:13:35 +08:00
```js
const { johnDoe: { age: userAge, email: userEmail }} = user;
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Replace the two assignments with an equivalent destructuring assignment. It should still assign the variables `lowToday` and `highToday` the values of `today.low` and `today.high` from the `LOCAL_FORECAST` 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(/lowToday = LOCAL_FORECAST\.today\.low/g) &&
!code.match(/highToday = LOCAL_FORECAST\.today.high/g)
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should use destructuring to create the `lowToday` variable.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
code.match(
/(var|const|let)\s*{\s*today\s*:\s*{\s*(low\s*:\s*lowToday[^}]*|[^,]*,\s*low\s*:\s*lowToday\s*)}\s*}\s*=\s*LOCAL_FORECAST(;|\s+|\/\/)/g
)
);
```
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
You should use destructuring to create the `highToday` variable.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
```js
assert(
code.match(
/(var|const|let)\s*{\s*today\s*:\s*{\s*(high\s*:\s*highToday[^}]*|[^,]*,\s*high\s*:\s*highToday\s*)}\s*}\s*=\s*LOCAL_FORECAST(;|\s+|\/\/)/g
)
);
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
`lowToday` should be equal to `64` and `highToday` should be equal to `77` .
```js
assert(lowToday === 64 & & highToday === 77);
```
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
const LOCAL_FORECAST = {
yesterday: { low: 61, high: 75 },
today: { low: 64, high: 77 },
tomorrow: { low: 68, high: 80 }
};
// Only change code below this line
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
const lowToday = LOCAL_FORECAST.today.low;
const highToday = LOCAL_FORECAST.today.high;
// 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 LOCAL_FORECAST = {
yesterday: { low: 61, high: 75 },
today: { low: 64, high: 77 },
tomorrow: { low: 68, high: 80 }
};
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
const { today: { low: lowToday, high: highToday }} = LOCAL_FORECAST;
```