2.0 KiB
2.0 KiB
id, title, challengeType, videoUrl, forumTopicId, localeTitle
id | title | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|---|
56592a60ddddeae28f7aa8e1 | Access Multi-Dimensional Arrays With Indexes | 1 | https://scrimba.com/c/ckND4Cq | 16159 | 使用索引访问多维数组 |
Description
[index]
访问的是第 N 个子数组,第二个[index]
访问的是第 N 个子数组的第N个元素。
示例
var arr = [
[1,2,3],
[4,5,6],
[7,8,9],
[[10,11,12], 13, 14]
];
arr[3]; // equals [[10,11,12], 13, 14]
arr[3][0]; // equals [10,11,12]
arr[3][0][1]; // equals 11
提示
数组名称和方括号之间不应该有任何空格,如array [0][0]
,甚至array [0] [0]
,都是不正确的。尽管 JavaScript 能够处理,但可能会让看你代码的其他程序员感到困惑。
Instructions
myArray
选择一个元素,使得myData
的值为8
。
Tests
tests:
- text: <code>myData</code>应该等于<code>8</code>。
testString: assert(myData === 8);
- text: 你应该使用方括号从<code>myArray</code>中取值。
testString: 'assert(/myArray\[2\]\[1\]/g.test(code) && !/myData\s*=\s*(?:.*[-+*/%]|\d)/g.test(code));'
Challenge Seed
// Setup
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];
// Only change code below this line.
var myData = myArray[0][0];
After Test
if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " myArray: " + JSON.stringify(myArray);})();}
Solution
var myArray = [[1,2,3],[4,5,6], [7,8,9], [[10,11,12], 13, 14]];
var myData = myArray[2][1];