Assigning object properties to a variable (Accessing Nested Objects)

Hey guys, I am having trouble assigning object properties to a variable. The challenge that I am specifically working on is: Basic Java - Accessing Nested Objects.

I have been trying to assign the property (“glove box”) of the object (myStorage) to the new variable (gloveBoxContents) but, either I am getting [object.Object] or Undefined.

I know that code is read/assigned/declared from right to left, but I am struggling with this one and how to structure it. Any help would be greatly appreciated.

Thanks,

Jake

Do you understand how the example works?

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"

I am not sure. This is what I think I know.

ourStorage (Object), “.” or dot notation < calls on the property cabinet and property [“top drawer”]. Top drawer is in bracket notation because there is a space between the words “Top_drawer”. folder 2 holds the value “secrets”.

Okay, and how are you trying to assign the property currently?

If I was to right it similar to the example I would do something like this. Keeping in mind storing the value of the property into the new variable.

var gloveBoxContent = myStorage.car.inside[“glove box”]; (compiles as: can’t find variable gloveBoxContent

OR
var gloveBoxContent = [“glove box”] inside.car.myStorage;
Thanks for the response!

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

// Only change code below this lin
var gloveBoxContent = myStorage.car.inside["glove box"]; // Change this line
1 Like

Thanks for the response Dawson

It could be the " // Only change code below this lin " missing the “e” . The way you have accessed it looks correct. Would you try correcting that and see if you get the solution?

Just checked, and nope, that wasn’t it :confused:

I see one more typo! Carefully look at the instructions and look at your error message closely. See if you can find it! Then you should be good to go :slight_smile:

LOL! Addisonday-thankyou. Wow…

Dawson thank you for the help too. This community is great. Truly much appreciated. Wow, that was so little but critical.

thanks JakeVittoriso

How would I bracket several words? For example, suppose instead of this:

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

If “inside” was “inside car”
Can you do this:

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

I am just wondering - it seems like the bracket was useful here because glove box is 2 words, and so using the dot notation would not work. But, suppose if you have 2 phrases next to each other, how do you properly notate?

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

var gloveBoxContents = myStorage.car.inside[“glove box”]; // Change this line

Hahaha I was doing similar and wondering why it wasn’t working. Read this thread and checked my code again. Had “glove box” capitalized “Glove box”. Changed Glove to glove and passed the tests finally!!! : )