freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-data-structures/access-an-arrays-contents-u...

3.0 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
5a661e0f1068aca922b3ef17 Access an Array's Contents Using Bracket Notation 1 使用括号表示法访问数组的内容

Description

当然任何数据结构的基本特征是不仅能够存储数据而且能够根据命令检索该数据。所以既然我们已经学会了如何创建数组那么让我们开始考虑如何访问该数组的信息。当我们定义一个如下所示的简单数组时其中有3个项目
让ourArray = [“a”“b”“c”];
在数组中,每个数组项都有一个索引 。此索引兼作数组中该项的位置以及您如何引用它。但是值得注意的是JavaScript数组是零索引的 ,这意味着数组的第一个元素实际上处于第位,而不是第一个。为了从数组中检索元素,我们可以将一个索引括在括号中,并将其附加到数组的末尾,或者更常见的是附加到引用数组对象的变量。这称为括号表示法 。例如,如果我们想从ourArray检索"a"并将其分配给变量,我们可以使用以下代码执行此操作:
让ourVariable = ourArray [0];
// ourVariable等于“a”
除了访问与索引相关的值,你还可以设置索引使用相同的符号中的值:
ourArray [1] =“不再是b”;
// ourArray现在等于[“a”“不再b”“c”];
使用括号表示法我们现在将索引1处的项目从"b"重置为"not b anymore"

Instructions

为了完成此挑战,将myArray的第二个位置(索引1 )设置为您想要的任何内容,除了"b"

Tests

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>");'

Challenge Seed

let myArray = ["a", "b", "c", "d"];
// change code below this line

//change code above this line
console.log(myArray);

Solution

// solution required