freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/accessing-nested-arrays.md

1.9 KiB
Raw Blame History

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];
ourPets[1].names[0];

ourPets[0].names[1] 应该是字符串 Fluffy 并且 ourPets[1].names[0] 应该是字符串 Spot

--instructions--

使用对象的点号和数组的方括号从变量 myPlants 检索出第二棵树。

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