Files
2022-01-20 20:30:18 +01:00

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

これまでの例で見てきたとおり、オブジェクトには、ネストされたオブジェクトとネストされた配列のどちらも含めることができます。 ネストされたオブジェクトへのアクセスと同様に、配列のブラケット記法のチェーンを使用して、ネストされた配列にアクセスできます。

次はネストされた配列にアクセスする方法の例です。

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

ドット記法とブラケット記法を使用して、変数 secondTreemyPlants オブジェクトの trees リストの 2 番目のアイテムとなるように設定してください。

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

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

const secondTree = "";

--solutions--

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

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