dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
112 lines
2.0 KiB
Markdown
112 lines
2.0 KiB
Markdown
---
|
|
id: 56533eb9ac21ba0edf2244cc
|
|
title: Accessing Nested Objects
|
|
challengeType: 1
|
|
forumTopicId: 16161
|
|
dashedName: accessing-nested-objects
|
|
---
|
|
|
|
# --description--
|
|
|
|
The sub-properties of objects can be accessed by chaining together the dot or bracket notation.
|
|
|
|
Here is a nested object:
|
|
|
|
```js
|
|
const ourStorage = {
|
|
"desk": {
|
|
"drawer": "stapler"
|
|
},
|
|
"cabinet": {
|
|
"top drawer": {
|
|
"folder1": "a file",
|
|
"folder2": "secrets"
|
|
},
|
|
"bottom drawer": "soda"
|
|
}
|
|
};
|
|
|
|
ourStorage.cabinet["top drawer"].folder2;
|
|
ourStorage.desk.drawer;
|
|
```
|
|
|
|
`ourStorage.cabinet["top drawer"].folder2` would be the string `secrets`, and `ourStorage.desk.drawer` would be the string `stapler`.
|
|
|
|
# --instructions--
|
|
|
|
Access the `myStorage` object and assign the contents of the `glove box` property to the `gloveBoxContents` variable. Use dot notation for all properties where possible, otherwise use bracket notation.
|
|
|
|
# --hints--
|
|
|
|
`gloveBoxContents` should equal the string `maps`.
|
|
|
|
```js
|
|
assert(gloveBoxContents === 'maps');
|
|
```
|
|
|
|
Your code should use dot notation, where possible, to access `myStorage`.
|
|
|
|
```js
|
|
assert.match(code, /myStorage\.car\.inside/);
|
|
```
|
|
|
|
`gloveBoxContents` should still be declared with `const`.
|
|
|
|
```js
|
|
assert.match(code, /const\s+gloveBoxContents\s*=/);
|
|
```
|
|
|
|
You should not change the `myStorage` object.
|
|
|
|
```js
|
|
const expectedMyStorage = {
|
|
"car":{
|
|
"inside":{
|
|
"glove box":"maps",
|
|
"passenger seat":"crumbs"
|
|
},
|
|
"outside":{
|
|
"trunk":"jack"
|
|
}
|
|
}
|
|
};
|
|
assert.deepStrictEqual(myStorage, expectedMyStorage);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
const myStorage = {
|
|
"car": {
|
|
"inside": {
|
|
"glove box": "maps",
|
|
"passenger seat": "crumbs"
|
|
},
|
|
"outside": {
|
|
"trunk": "jack"
|
|
}
|
|
}
|
|
};
|
|
|
|
const gloveBoxContents = undefined;
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
const myStorage = {
|
|
"car":{
|
|
"inside":{
|
|
"glove box":"maps",
|
|
"passenger seat":"crumbs"
|
|
},
|
|
"outside":{
|
|
"trunk":"jack"
|
|
}
|
|
}
|
|
};
|
|
const gloveBoxContents = myStorage.car.inside["glove box"];
|
|
```
|