2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244cd
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cLeGDtZ'
|
|
|
|
forumTopicId: 16160
|
2018-10-10 18:03:03 -04:00
|
|
|
localeTitle: 访问嵌套数组
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
正如我们在前面的例子所见,对象可以嵌套对象和数组。与访问嵌套对象一样,用中括号操作符同样可以访问嵌套数组。
|
|
|
|
下面是如何访问嵌套数组的例子:
|
|
|
|
|
|
|
|
```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"
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
使用点操作符和中括号操作符来检索变量<code>myPlants</code>的第二棵树。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>secondTree</code>应该等于 "pine"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(secondTree === "pine");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 使用点操作符和中括号操作符来检索变量<code>myPlants</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```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
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
(function(x) {
|
|
|
|
if(typeof x != 'undefined') {
|
|
|
|
return "secondTree = " + x;
|
|
|
|
}
|
|
|
|
return "secondTree is undefined";
|
|
|
|
})(secondTree);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
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];
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|