2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244cc
title: Accessing Nested Objects
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cRnRnfa'
2019-07-31 11:32:23 -07:00
forumTopicId: 16161
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
The sub-properties of objects can be accessed by chaining together the dot or bracket notation.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
Here is a nested object:
2019-05-17 06:20:30 -07:00
```js
var ourStorage = {
"desk": {
"drawer": "stapler"
},
"cabinet": {
"top drawer": {
"folder1": "a file",
"folder2": "secrets"
},
"bottom drawer": "soda"
}
};
ourStorage.cabinet["top drawer"].folder2; // "secrets"
ourStorage.desk.drawer; // "stapler"
```
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
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.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`gloveBoxContents` should equal "maps".
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(gloveBoxContents === 'maps');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
Your code should use dot and bracket notation to access `myStorage` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
## --after-user-code--
```js
(function(x) {
if(typeof x != 'undefined') {
return "gloveBoxContents = " + x;
}
return "gloveBoxContents is undefined";
})(gloveBoxContents);
```
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
// Setup
var myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
var gloveBoxContents = undefined; // Change this line
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2018-10-08 01:01:53 +01:00
var myStorage = {
"car":{
"inside":{
2018-09-30 23:01:58 +01:00
"glove box":"maps",
"passenger seat":"crumbs"
},
2018-10-08 01:01:53 +01:00
"outside":{
2018-09-30 23:01:58 +01:00
"trunk":"jack"
}
}
};
var gloveBoxContents = myStorage.car.inside["glove box"];
```