2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56592a60ddddeae28f7aa8e1
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/ckND4Cq'
|
|
|
|
forumTopicId: 16159
|
|
|
|
localeTitle: 使用索引访问多维数组
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
可以把 <dfn>多维</dfn> 数组看作成是一个 <em>数组中的数组</em>。当使用方括号去访问数组的时候,第一个<code>[index]</code>访问的是第 N 个子数组,第二个<code>[index]</code>访问的是第 N 个子数组的第N个元素。
|
|
|
|
<strong>示例</strong>
|
|
|
|
|
|
|
|
```js
|
|
|
|
var arr = [
|
|
|
|
[1,2,3],
|
|
|
|
[4,5,6],
|
|
|
|
[7,8,9],
|
|
|
|
[[10,11,12], 13, 14]
|
|
|
|
];
|
|
|
|
arr[3]; // equals [[10,11,12], 13, 14]
|
|
|
|
arr[3][0]; // equals [10,11,12]
|
|
|
|
arr[3][0][1]; // equals 11
|
|
|
|
```
|
|
|
|
|
|
|
|
<strong>提示</strong><br>数组名称和方括号之间不应该有任何空格,如<code>array [0][0]</code>,甚至<code>array [0] [0]</code>,都是不正确的。尽管 JavaScript 能够处理,但可能会让看你代码的其他程序员感到困惑。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
使用索引从<code>myArray</code>选择一个元素,使得<code>myData</code>的值为<code>8</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>myData</code>应该等于<code>8</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(myData === 8);
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 你应该使用方括号从<code>myArray</code>中取值。
|
|
|
|
testString: 'assert(/myArray\[2\]\[1\]/g.test(code) && !/myData\s*=\s*(?:.*[-+*/%]|\d)/g.test(code));'
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
|
|
|
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];
|
|
|
|
|
|
|
|
// Only change code below this line.
|
|
|
|
var myData = myArray[0][0];
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " myArray: " + JSON.stringify(myArray);})();}
|
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 myArray = [[1,2,3],[4,5,6], [7,8,9], [[10,11,12], 13, 14]];
|
|
|
|
var myData = myArray[2][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>
|