Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
access-an-arrays-contents-using-bracket-notation.chinese.md
access-property-names-with-bracket-notation.chinese.md
add-items-to-an-array-with-push-and-unshift.chinese.md
add-items-using-splice.chinese.md
add-key-value-pairs-to-javascript-objects.chinese.md
check-for-the-presence-of-an-element-with-indexof.chinese.md
check-if-an-object-has-a-property.chinese.md
combine-arrays-with-the-spread-operator.chinese.md
copy-an-array-with-the-spread-operator.chinese.md
copy-array-items-using-slice.chinese.md
create-complex-multi-dimensional-arrays.chinese.md
generate-an-array-of-all-object-keys-with-object.keys.chinese.md
iterate-through-all-an-arrays-items-using-for-loops.chinese.md
iterate-through-the-keys-of-an-object-with-a-for...in-statement.chinese.md
modify-an-array-stored-in-an-object.chinese.md
modify-an-object-nested-within-an-object.chinese.md
remove-items-from-an-array-with-pop-and-shift.chinese.md
remove-items-using-splice.chinese.md
use-an-array-to-store-a-collection-of-data.chinese.md
use-the-delete-keyword-to-remove-object-properties.chinese.md
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
english
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-data-structures/access-an-arrays-contents-using-bracket-notation.chinese.md

60 lines
3.0 KiB
Markdown
Raw Normal View History

---
id: 5a661e0f1068aca922b3ef17
title: Access an Array's Contents Using Bracket Notation
challengeType: 1
videoUrl: ''
localeTitle: 使用括号表示法访问数组的内容
---
## Description
<section id="description">当然任何数据结构的基本特征是不仅能够存储数据而且能够根据命令检索该数据。所以既然我们已经学会了如何创建数组那么让我们开始考虑如何访问该数组的信息。当我们定义一个如下所示的简单数组时其中有3个项目 <blockquote>让ourArray = [“a”“b”“c”]; </blockquote>在数组中,每个数组项都有一个<dfn>索引</dfn> 。此索引兼作数组中该项的位置以及您如何引用它。但是值得注意的是JavaScript数组是<dfn>零索引的</dfn> ,这意味着数组的第一个元素实际上处于第<em><strong></strong></em>位,而不是第一个。为了从数组中检索元素,我们可以将一个索引括在括号中,并将其附加到数组的末尾,或者更常见的是附加到引用数组对象的变量。这称为<dfn>括号表示法</dfn> 。例如,如果我们想从<code>ourArray</code>检索<code>&quot;a&quot;</code>并将其分配给变量,我们可以使用以下代码执行此操作: <blockquote>让ourVariable = ourArray [0]; <br> // ourVariable等于“a” </blockquote>除了访问与索引相关的值,你还可以<em>设置</em>索引使用相同的符号中的值: <blockquote> ourArray [1] =“不再是b”; <br> // ourArray现在等于[“a”“不再b”“c”]; </blockquote>使用括号表示法我们现在将索引1处的项目从<code>&quot;b&quot;</code>重置为<code>&quot;not b anymore&quot;</code></section>
## Instructions
<section id="instructions">为了完成此挑战,将<code>myArray</code>的第二个位置(索引<code>1</code> )设置为您想要的任何内容,除了<code>&quot;b&quot;</code></section>
## Tests
<section id='tests'>
```yml
tests:
- text: '<code>myArray[0]</code>等于<code>&quot;a&quot;</code>'
testString: 'assert.strictEqual(myArray[0], "a", "<code>myArray[0]</code> is equal to <code>"a"</code>");'
- text: '<code>myArray[1]</code>不再设置为<code>&quot;b&quot;</code>'
testString: 'assert.notStrictEqual(myArray[1], "b", "<code>myArray[1]</code> is no longer set to <code>"b"</code>");'
- text: '<code>myArray[2]</code>等于<code>&quot;c&quot;</code>'
testString: 'assert.strictEqual(myArray[2], "c", "<code>myArray[2]</code> is equal to <code>"c"</code>");'
- text: '<code>myArray[3]</code>等于<code>&quot;d&quot;</code>'
testString: 'assert.strictEqual(myArray[3], "d", "<code>myArray[3]</code> is equal to <code>"d"</code>");'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
let myArray = ["a", "b", "c", "d"];
// change code below this line
//change code above this line
console.log(myArray);
```
</div>
</section>
## Solution
<section id='solution'>
```js
// solution required
```
</section>