Files
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-multi-dimensional-arrays-with-indexes.md
2022-10-26 12:51:22 -07:00

1.9 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56592a60ddddeae28f7aa8e1 使用索引访问多维数组 1 https://scrimba.com/c/ckND4Cq 16159 access-multi-dimensional-arrays-with-indexes

--description--

我们可以把多维数组看作成是数组中的数组。 When you use brackets to access your array, the first set of brackets refers to the entries in the outermost (the first level) array, and each additional pair of brackets refers to the next level of entries inside.

例如:

const arr = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [[10, 11, 12], 13, 14]
];

const subarray = arr[3];
const nestedSubarray = arr[3][0];
const element = arr[3][0][1];

在这个例子中,subarray 的值为 [[10, 11, 12], 13, 14] nestedSubarray 的值为 [10, 11, 12]element 的值为 11

注意: 数组名与方括号之间不应该有任何空格,比如 array [0][0] 甚至是 array [0] [0] 都是不允许的。 尽管 JavaScript 能够正确处理这种情况,但是当其他程序员阅读你写的代码时,这可能让他们感到困惑。

--instructions--

使用方括号从 myArray 中选取一个值,使得 myData 等于 8

--hints--

myData 应该等于 8

assert(myData === 8);

你应该使用方括号从 myArray 中读取正确的值。

assert(/myData=myArray\[2\]\[1\]/.test(__helpers.removeWhiteSpace(code)));

--seed--

--after-user-code--

if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " myArray: " + JSON.stringify(myArray);})();}

--seed-contents--

const myArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [[10, 11, 12], 13, 14],
];

const myData = myArray[0][0];

--solutions--

const myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [[10, 11, 12], 13, 14]];
const myData = myArray[2][1];