Accessing Nested Objects


#1

Hello!

So for the Javascript challenges, I am stuck in the “Accessing Nested Objects” part.

The sub-properties of objects can be accessed by chaining together the dot or bracket notation.

Here is a nested object:

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"

Instructions
Access the myStorage object and assign the contents of the glove box property to the gloveBoxContents variable. Use bracket notation for properties with a space in their name.

I don’t know if it’s because I don’t understand english ? I tried doing this

// Setup
var myStorage = {
  "car": {
    "inside": {
      "glove box": "maps",
      "passenger seat": "crumbs"
     },
    "outside": {
      "trunk": "jack"
    }
  }
};

// Only change code below this line
myStorage.car.inside["glove box"];

var gloveBoxContents = ""; // Change this line

But I am still stuck. Can somebody help me please ? Plus, when I click on the get hints I can’t find anything… :frowning:


#2

You are close. The instructions say to assign the contents of the glove box property to the gloveBoxContents variable. You currently have assigned a blank string to gloveBoxContents. You just need to move one line to a part of another.


#3

I actually feel stupid right now. -_- I kept doing that but got stuck with an error saying dot notation something like that. But now it’s all okay ! Thank you! :smiley:


#4

how you slove this, can you send me the code


#5

Instead of asking for the solution, why not post the code you have so far for the challenge and ask questions for parts you are having trouble understanding.


#6

Solution: var gloveBoxContents = myStorage.car.inside["glove box"];


#7

Thanks I couldnt figure it out I have them on separate lines.


#8

var gloveBoxContents = myStorage.car.inside[“glove box”];


#9

Question:
This code doesn’t work because there is a space in glove box correct that is why you use the brackets?
var myStorage = {
“car”: {
“inside”: {
“glove box”: “maps”,
“passenger seat”: “crumbs”
},
“outside”: {
“trunk”: “jack”
}
}
};

// Only change code below this line

var gloveBoxContents = myStorage.car[“inside”].glove box;