2020-10-06 23:10:08 +05:30

2.0 KiB

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

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的第二棵树。

Tests

tests:
  - text: <code>secondTree</code>应该等于 "pine"。
    testString: assert(secondTree === "pine");
  - text: 使用点操作符和中括号操作符来检索变量<code>myPlants</code>。
    testString: assert(/=\s*myPlants\[1\].list\[1\]/.test(code));

Challenge Seed

// 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

After Test

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

Solution

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];