mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-06 15:03:08 -05:00
1.8 KiB
1.8 KiB
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--
我們可以把多維數組看作成是數組中的數組。 當你使用括號訪問你的數組時,第一組括號指的是最外層(第一層)數組中的條目,而每一對額外的括號指的是裏面下一層的條目。
例如:
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];