Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* 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>
2021-01-12 19:31:00 -07:00

83 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 5a661e0f1068aca922b3ef17
title: 使用方括号访问数组的元素
challengeType: 1
forumTopicId: 301149
dashedName: access-an-arrays-contents-using-bracket-notation
---
# --description--
所有数据结构的基本特性是,它们不仅可以存储数据,还可以让我们按需访问存放在其中的数据。我们已经学习了如何创建数组,现在让我们来学习如何访问数组中的数据。
我们先定义一个包含 3 个元素的数组:
```js
let ourArray = ["a", "b", "c"];
```
在数组中,内部的每个元素都有一个与之对应的<dfn>索引</dfn><dfn>index</dfn>。索引既是该元素在数组中的位置也是我们访问该元素的参考。需要注意的是JavaScript 数组的索引是从 0 开始的(这种从 0 开始的规则叫做 <dfn>zero-indexed</dfn>),即数组的第一个元素是在数组中的***第 0 个***位置,而不是第 1 个位置。要从数组中获取一个元素,我们可以在数组字面量后面加一个用方括号(`[]`)括起来的索引。不过习惯上,我们会通过表示数组的变量名来访问,而不是直接通过字面量。这种从数组中读取元素的方式叫做<dfn>方括号表示法</dfn><dfn>bracket notation</dfn>)。如果我们要从数组 `ourArray` 中获取数据元素 `"a"` 并将其赋值给另一个变量,可以这样写:
```js
let ourVariable = ourArray[0];
// ourVariable 的值为 "a"
```
除了使用索引来获取某个元素值以外,你还可以通过类似的写法来*设置*一个索引位置的元素值:
```js
ourArray[1] = "not b anymore";
// ourArray 现在的值为 ["a", "not b anymore", "c"];
```
在上面的代码中,我们用方括号表示法把索引为 1 的元素从 `"b"` 改成了 `"not b anymore"`
# --instructions--
在本挑战中,请将 `myArray` 中的第二个元素(索引为 `1`)设置为除了 `"b"` 以外的任意值。
# --hints--
`myArray[0]` 应为 `"a"`
```js
assert.strictEqual(myArray[0], 'a');
```
`myArray[1]` 不应为 `"b"`
```js
assert.notStrictEqual(myArray[1], 'b');
```
`myArray[2]` 应为 `"c"`
```js
assert.strictEqual(myArray[2], 'c');
```
`myArray[3]` 应为 `"d"`
```js
assert.strictEqual(myArray[3], 'd');
```
# --seed--
## --seed-contents--
```js
let myArray = ["a", "b", "c", "d"];
// Only change code below this line
// Only change code above this line
console.log(myArray);
```
# --solutions--
```js
let myArray = ["a", "b", "c", "d"];
myArray[1] = "e";
```