Files
2021-10-27 21:47:35 +05:30

1.9 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244cc 訪問嵌套對象 1 https://scrimba.com/c/cRnRnfa 16161 accessing-nested-objects

--description--

我們可以通過連續使用點號表示法和方括號表示法來訪問對象的嵌套屬性。

這是一個嵌套對象:

const ourStorage = {
  "desk": {
    "drawer": "stapler"
  },
  "cabinet": {
    "top drawer": { 
      "folder1": "a file",
      "folder2": "secrets"
    },
    "bottom drawer": "soda"
  }
};

ourStorage.cabinet["top drawer"].folder2;
ourStorage.desk.drawer;

ourStorage.cabinet["top drawer"].folder2 將會是字符串 secrets,並且 ourStorage.desk.drawer 將會是字符串 stapler

--instructions--

訪問 myStorage 對象並將 glove box 屬性的內容賦值給 gloveBoxContents 變量。 在可能的情況下,對所有的屬性使用點號,否則使用方括號。

--hints--

gloveBoxContents 應該等於字符串 maps

assert(gloveBoxContents === 'maps');

你的代碼應該使用點號和方括號來訪問 myStorage

assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));

--seed--

--after-user-code--

(function(x) { 
  if(typeof x != 'undefined') { 
    return "gloveBoxContents = " + x;
  }
  return "gloveBoxContents is undefined";
})(gloveBoxContents);

--seed-contents--

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

const gloveBoxContents = undefined;

--solutions--

const myStorage = {
  "car":{
    "inside":{
      "glove box":"maps",
      "passenger seat":"crumbs"
    },
    "outside":{
      "trunk":"jack"
    }
  }
};
const gloveBoxContents = myStorage.car.inside["glove box"];