Accessing nested objects need help

Tell us what’s happening:
I’m getting an error, can anyone help?

  **Your code so far**

var myStorage = {
"car": {
  "inside": {
    "glove box": "maps",
    "passenger seat": "crumbs"
   },
  "outside": {
    "trunk": "jack"
  }
}
};
var gloveBoxContents = myStorage.car["inside"].glovebox;

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15

Challenge: Accessing Nested Objects

Link to the challenge:

var gloveBoxContents = myStorage.car["inside"].glovebox;

You only need bracket notation in two cases:

  1. The property name is stored in a variable.
  2. The property name is not a valid JS identifier (make up of letters, numbers, _, or $ and does not start with a number).

The first case is not an issue here, but we do have an issue with “glove box” - it is not a valid JS identifier because it has a space in it. You need to do what you did for “inside” for this property. But the “inside” property should just be dot notation.

When I do that, your code passes for me.

I did this and it still failed var gloveBoxContents = myStorage.car.inside[glovebox];

That is closer. But you still have the problem with the “glove box”. The property name has a space in it, but you’ve done it as if it is a variable name. So, to make sure it hits the property in the array, it needs to be a string and that space needs to be in there.

1 Like

got it, ill put this in my notes so I’ll remember

You’re not the first to be confused by this. I used to work next two a couple junior devs that seemed constantly confused about this. I think as long as you understand what a valid JS identifier is and understand the two use cases I mentioned, it becomes clearer.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.