Accessing Nested Objects

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:

1 Like

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.

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:

how you slove this, can you send me the code

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.

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

4 Likes

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

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

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;

You are correct. Object properties with spaces in their names require the use of brackets.

4 Likes

ahhh, thanks so much. I was looking for an explanation and couldn’t find one until now. Thank you!

That makes perfect sense that a string with a space must use bracket notation, but to be devil’s advocate wouldn’t it be fine to use var gloveBoxContents = myStorage.car[“inside”]["glove box"]; or even var gloveBoxContents = myStorage["car"].inside["glove box"];? Both console.log(gloveBoxContents) to “maps” in Chrome console, but they don’t pass the FCC test. Really confusing what best practices are for dot notation versus bracket (except when there’s a string space or you’re trying to access a variable which both require bracket if I understand correctly).

Here you have the wrong kind of double quote

It may also be that the tests are checking what is written so there are limited ways for solution (as someone could just try to pass the tests assigning directly "maps" to that variable)

1 Like

Thx much for the response. I’m not sure why my quotes didn’t format correctly in the forum (they were correct in FCC and Chrome console), and I completely understand FCC not anticipating every way a solution could work. I guess my point is that an explanation on when to use dot notation vs. bracket would be helpful.

@Chiggens It turns out @ilenia is absolutely correct above that the last challenge test is expecting you to access both “car” and inside properties with dot notation with only “glove box” being accessed via bracket notation. If we want to use the same test, then I believe we should updated the instructions to make it clearer to the camper to use dot notation for all properties which do not have a space in them.

What would you think about the following instruction change?

Access the myStorage object and assign the contents of the glove box property to the gloveBoxContents variable. Use dot notation for all properties where possible, otherwise use bracket notation.

The challenge could be modified to state the following (which would allow your solution), but the test would have to be changed significantly (which is totally possible because I have already written it):

Access the myStorage object and assign the contents of the glove box property to the gloveBoxContents variable. Use any combination of dot and bracket notation which would allow you to access the contents.

I personally believe the first verbiage is better, because you should always use dot notation unless you can not. It just makes the code more readable.

1 Like

That’s awesome, and like I said, it was more of a devil’s advocate thing to ensure that they were indeed interchangeable if the string doesn’t have a space. I may be the only camper a bit confused by the ‘rules/best practices’ of dot vs. bracket.

you should always use dot notation unless you can not. It just makes the code more readable

That line was exactly the knowledge I was looking for. Thank you!

I have created this issue on GitHub to get some feedback about my suggested change.

1 Like