zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Accessing Nested Objects #258

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

介绍

257 灵活的数据结构

对象的自己属性可以通过逗号链或中括号链进行访问。

方法

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.

代码


// Setup
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"]; // Change this line

结果显示

image

来源

https://www.freecodecamp.org/challenges/accessing-nested-objects