2.3 KiB
2.3 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--
Використовуючи точкову і дужкову нотацію, встановіть змінну secondTree
на другий елемент в списку trees
з об'єкта 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--
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];