2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 56533eb9ac21ba0edf2244cd
|
2021-03-14 21:20:39 -06: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-03-14 21:20:39 -06:00
|
|
|
|
在之前的挑战中,我们学习了在对象中嵌套对象和数组。 与访问嵌套对象类似,数组的方括号可以用来对嵌套数组进行链式访问。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
下面是访问嵌套数组的例子:
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
|
const ourPets = [
|
2020-04-29 18:29:13 +08:00
|
|
|
|
{
|
|
|
|
|
animalType: "cat",
|
|
|
|
|
names: [
|
|
|
|
|
"Meowzer",
|
|
|
|
|
"Fluffy",
|
|
|
|
|
"Kit-Cat"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
animalType: "dog",
|
|
|
|
|
names: [
|
|
|
|
|
"Spot",
|
|
|
|
|
"Bowser",
|
|
|
|
|
"Frankie"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
];
|
2021-10-27 15:10:57 +00:00
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
ourPets[0].names[1];
|
|
|
|
|
ourPets[1].names[0];
|
2020-04-29 18:29:13 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
|
`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-05-10 01:12:02 +05:30
|
|
|
|
使用点和方括号,将变量 `secondTree` 的值设置为 `myPlants` 对象中 `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-03-14 21:20:39 -06: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-03-14 21:20:39 -06: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
|
2021-10-27 15:10:57 +00:00
|
|
|
|
const myPlants = [
|
2021-01-13 03:31:00 +01:00
|
|
|
|
{
|
|
|
|
|
type: "flowers",
|
|
|
|
|
list: [
|
|
|
|
|
"rose",
|
|
|
|
|
"tulip",
|
|
|
|
|
"dandelion"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "trees",
|
|
|
|
|
list: [
|
|
|
|
|
"fir",
|
|
|
|
|
"pine",
|
|
|
|
|
"birch"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
2021-10-27 15:10:57 +00:00
|
|
|
|
const secondTree = "";
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```
|
|
|
|
|
|
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
|
2021-10-27 15:10:57 +00:00
|
|
|
|
const myPlants = [
|
2021-01-13 03:31:00 +01:00
|
|
|
|
{
|
|
|
|
|
type: "flowers",
|
|
|
|
|
list: [
|
|
|
|
|
"rose",
|
|
|
|
|
"tulip",
|
|
|
|
|
"dandelion"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "trees",
|
|
|
|
|
list: [
|
|
|
|
|
"fir",
|
|
|
|
|
"pine",
|
|
|
|
|
"birch"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
2021-10-27 15:10:57 +00:00
|
|
|
|
const secondTree = myPlants[1].list[1];
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```
|