Assigning object properties to a variable (Accessing Nested Objects)

Assigning object properties to a variable (Accessing Nested Objects)
0

#1

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


#2

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"

#3

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”.


#4

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


#5

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!


#6

Can you post your entire code into the post, just so we know exactly what you are trying. It may be a typo somewhere.


#8
// 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

#9

Thanks for the response Dawson


#10

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?


#11

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


#12

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:


#13

Like @addisonday says, you have a typo. Look at the variable name you wrote compared to what the instructions ask to you use for the variable name. It is one letter off.


#14

LOL! Addisonday-thankyou. Wow…


#15

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


#16

@JakeVittoriso - Glad we could be of assistance. Typos are one of the biggest sources of errors when programming.

FYI - You might have noticed that I edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


#17

thanks JakeVittoriso


#18

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


#19

Not exactly sure what you mean by 2 phrases next to each other. Can you show an example object and tell us what property value you are trying to reference?


#20

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!!! : )