Basic JavaScript: Accessing Nested Objects

Basic JavaScript: Accessing Nested Objects

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"

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.

SOLUTION:----------------------- 


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

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

Comments

Popular posts from this blog

Basic CSS: Use Attribute Selectors to Style Elements

Basic CSS: Use Clockwise Notation to Specify the Padding of an Element

Basic CSS: Use Clockwise Notation to Specify the Margin of an Element