2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56bbb991ad1ed5201cd392ca
|
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/cBZQbTz'
|
|
|
|
forumTopicId: 16158
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: access-array-data-with-indexes
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
|
|
|
我们可以使用索引 `indexes` 来访问数组中的数据。
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
数组索引与字符串索引一样使用中括号,但字符串索引得到的是一个字符,而数组索引得到的是一个元素。数组索引与字符串索引一样是从 0 开始的,所以数组中第一个元素的索引编号是 0。
|
|
|
|
**示例**
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
var array = [50,60,70];
|
|
|
|
array[0]; // equals 50
|
|
|
|
var data = array[1]; // equals 60
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
**提示**
|
|
|
|
数组名称和方括号之间不应有任何空格,如`array [0]`尽管 JavaScript 能够正确处理,但可能会让看你代码的其他程序员感到困惑
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
创建一个名为`myData`的变量,并把`myArray`的第一个索引上的值赋给它。
|
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
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
变量`myData`的值应该等于`myArray`的第一个值。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(
|
|
|
|
(function () {
|
|
|
|
if (
|
|
|
|
typeof myArray !== 'undefined' &&
|
|
|
|
typeof myData !== 'undefined' &&
|
|
|
|
myArray[0] === myData
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
应使用方括号访问变量`myArray`中的数据。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(
|
|
|
|
(function () {
|
|
|
|
if (code.match(/\s*=\s*myArray\[0\]/g)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);}
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
|
|
|
var myArray = [50,60,70];
|
|
|
|
|
|
|
|
// Only change code below 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 myArray = [50,60,70];
|
|
|
|
var myData = myArray[0];
|
|
|
|
```
|