2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244cd
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 访问嵌套数组
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cLeGDtZ'
|
|
|
|
forumTopicId: 16160
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: accessing-nested-arrays
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2021-01-13 09:11:33 -08:00
|
|
|
在之前的挑战中,我们学习了在对象中嵌套对象和数组。与访问嵌套的对象一样,我们可以用方括号表示法来访问嵌套数组。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-01-13 09:11:33 -08:00
|
|
|
下面是访问嵌套数组的例子:
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
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"
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-13 09:11:33 -08:00
|
|
|
请使用点号表示法和方括号表示法来检索变量 `myPlants` 中的第二棵树,即返回 `type` 为 `trees` 的数组中的第二个元素。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-13 09:11:33 -08:00
|
|
|
`secondTree` 的值应为 "pine"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(secondTree === 'pine');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-13 09:11:33 -08:00
|
|
|
应使用点号表示法和方括号表示法来检索变量 `myPlants`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(x) {
|
|
|
|
if(typeof x != 'undefined') {
|
|
|
|
return "secondTree = " + x;
|
|
|
|
}
|
|
|
|
return "secondTree is undefined";
|
|
|
|
})(secondTree);
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// 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
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
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];
|
|
|
|
```
|