2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244cc
title: Accessing Nested Objects
challengeType: 1
2020-05-21 17:31:25 +02:00
isHidden: false
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
---
## Description
< section id = 'description' >
The sub-properties of objects can be accessed by chaining together the dot or bracket notation.
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"
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-05-22 03:37:50 +05:30
Access the < code > myStorage< / code > object and assign the contents of the < code > glove box< / code > property to the < code > gloveBoxContents< / code > variable. Use dot notation for all properties where possible, otherwise use bracket notation.
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-27 02:57:38 -08:00
- text: < code > gloveBoxContents</ code > should equal "maps".
2019-07-13 00:07:53 -07:00
testString: assert(gloveBoxContents === "maps");
2019-11-27 02:57:38 -08:00
- text: Your code should use dot and bracket notation to access < code > myStorage</ code > .
2019-07-13 00:07:53 -07:00
testString: assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Setup
var myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
var gloveBoxContents = undefined; // Change this line
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
(function(x) {
if(typeof x != 'undefined') {
return "gloveBoxContents = " + x;
}
return "gloveBoxContents is undefined";
})(gloveBoxContents);
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```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"];
```
< / section >