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

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

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;

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.

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!