* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
124 lines
1.8 KiB
Markdown
124 lines
1.8 KiB
Markdown
---
|
|
id: 56533eb9ac21ba0edf2244cd
|
|
title: 访问嵌套数组
|
|
challengeType: 1
|
|
videoUrl: 'https://scrimba.com/c/cLeGDtZ'
|
|
forumTopicId: 16160
|
|
dashedName: accessing-nested-arrays
|
|
---
|
|
|
|
# --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"
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
使用点操作符和中括号操作符来检索变量`myPlants`的第二棵树。
|
|
|
|
# --hints--
|
|
|
|
`secondTree`应该等于 "pine"。
|
|
|
|
```js
|
|
assert(secondTree === 'pine');
|
|
```
|
|
|
|
使用点操作符和中括号操作符来检索变量`myPlants`。
|
|
|
|
```js
|
|
assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
|
|
```
|
|
|
|
# --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
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```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];
|
|
```
|