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

2.0 KiB

id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244cd Accedere ad Array annidati 1 https://scrimba.com/c/cLeGDtZ 16160 accessing-nested-arrays

--description--

Come abbiamo visto in precedenti esempi, gli oggetti possono contenere sia oggetti nidificati che array nidificati. Simile all'accesso agli oggetti nidificati, la notazione a parentesi dell'array può essere concatenata per accedere agli array nidificati.

Ecco un esempio di come accedere a un array nidificato:

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] sarebbe la stringa Fluffy e ourPets[1].names[0] sarebbe la stringa Spot.

--instructions--

Usando la notazione a punto e a parentesi, imposta la variabile secondTree al secondo elemento nella lista trees dall'oggetto myPlants.

--hints--

secondTree dovrebbe essere uguale alla stringa pine.

assert(secondTree === 'pine');

Il tuo codice dovrebbe utilizzare la notazione a punti e parentesi per accedere a 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];