1.9 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244cd 访问嵌套数组 1 https://scrimba.com/c/cLeGDtZ 16160 accessing-nested-arrays

--description--

在之前的挑战中,我们学习了在对象中嵌套对象和数组。与访问嵌套的对象一样,我们可以用方括号表示法来访问嵌套数组。

下面是访问嵌套数组的例子:

var ourPets = [
  {
    animalType: "cat",
    names: [
      "Meowzer",
      "Fluffy",
      "Kit-Cat"
    ]
  },
  {
    animalType: "dog",
    names: [
      "Spot",
      "Bowser",
      "Frankie"
    ]
  }
];
ourPets[0].names[1]; // "Fluffy"
ourPets[1].names[0]; // "Spot"

--instructions--

请使用点号表示法和方括号表示法来检索变量 myPlants 中的第二棵树,即返回 typetrees 的数组中的第二个元素。

--hints--

secondTree 的值应为 "pine"。

assert(secondTree === 'pine');

应使用点号表示法和方括号表示法来检索变量 myPlants

assert(/=\s*myPlants\[1\].list\[1\]/.test(code));

--seed--

--after-user-code--

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

--seed-contents--

// Setup
var myPlants = [
  {
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  },
  {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  }
];

// Only change code below this line

var secondTree = ""; // Change this line

--solutions--

var myPlants = [
  {
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  },
  {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  }
];

// Only change code below this line

var secondTree = myPlants[1].list[1];