Getting stuck with: Accessing Nested Objects lesson

Tell us what’s happening:

I can’t pass the test

Your code so far


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

Your browser information:

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

Challenge: Accessing Nested Objects

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects

1 Like

the variable you assign to should be gloveBoxContents.

1 Like

you are accessing the contents of myStorage. So you have to zoom down using the dot notation until you reach the property my glove box. Or you could click get a hint.

So the point of the exercise is not to re-create the glove box bit - it’s to “map” the path inside the object, in order to get to the glove box property. Let’s try an example.

Suppose we had an object like this:

const user = {
  "name": {
    "first": "Bob",
    "middle initial": "T",
    "last": "Williams"
  },
  "grades":{
    "EN101": 3.75,
    "PSY106": 4.00,
    "TM100": 3.6
  }
}

Now, that user has a name property, which we can get to via user.name. We can use dot notation here, as the name property is a simple word, no spaces or reserved characters. If we wanted to get to Bob’s English grade, we could use user.grades.EN101, which would work fine.

However, if we wanted to retrieve Bob’s middle initial, user.name.middle initial won’t work - the space in there will cause javascript to throw an error (both that middle isn’t a property, and that initial is not a variable, probably). So we need another mechanism to access property names with special characters (like spaces). This would be bracket notation: user.name["middle initial"] would get us there. Note that we could do the same thing all the way down, user["name"]["middle initial"], but that’s a little more typing.

So, in the case of the myStorage object, the path goes:

myStorage -> car -> inside -> glove box

… Using dot notation, you can get to inside - but you’ll need to remember the space in glove box and find another way there. Can you turn that path I just outlined to make an accessor for the glove box property?

I will try to do so

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

myStorage.car["inside"].glove.box;
myStorage.car.glove.box;

start myStorage then car inside and ending with glove box

1 Like

Remember, you will want to reset the challenge back to the original. You’re only changing one line. Specifically, the line that looks like:

var gloveBoxContents = undefined; // Change this line

you need to change the undefined to the path you want to access. Now you know it’s somewhere in myStorage. that object has only one property, car, which we can see at 'myStorage.car`.

The myStorage.car (which is a nested object, that is one object tucked inside another) has two properties: inside and outside. We can access either of these by myStorage.car.inside or myStorage.car.outside.

Now, we get to the funky bit. myStorage.car.inside has two properties as well: "glove box" and "passenger seat". Now, these properties are not going to work with the dot notation we were using above. These will need to be in brackets, because of the space in the string. So, to get into the passenger seat, we could:

const passengerSeatContents = myStorage.car.inside["passenger seat"];

Can you do something similar to see the contents of the glove box? And set the variable gloveBoxContents to them?

1 Like

change the undefine to the path i want to acces
if i want to do so i need myStorage with one property that of car
so myStorage.car is a nested object meaning one object nested inside another object
inside/outside we can see this because it has an , which connect the two of them
we can acces them just like we did before start with myStorage then car and ending with either the inside/outside again using the dot notation.
So far so good
myStorage.car.inside also has two property’s: "glove box" and "passenger seat" because both of those are inside the curly brackets? we cannot just acces them and need to set up another set of brackets? nope thats not right they have a space in between them
yes let me give it a try

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

Thanks for the explanation this really helped.

1 Like

I like the way you broke that down and jotted down the thoughts behind each step, very well done!

As to your question,

Yes, because they are inside the curly braces:

// all the stuff before...
  "inside": {
    "glove box": "maps",
    "passenger seat": "crumbs"
  },
  // and more stuff after...

Those two are properties of the object inside. Good catch! But as you see, because of the space in the property name, we can’t simply dot-notate, we have to use the right-angle brackets, as you’ve done.

Glad it worked out, and keep asking great questions!