这是一个数组,其中包含索引0的问题和1-4中的选项,然后索引5包含答案。我想单独访问这些索引号但不能。
while (var < 10) {
// do something
var++;
}
&#13;
问题是当我调用[0,0]时,数组中的整个句子出现而不是数组中的一个元素。我的数组是关于位置0的问题集合以及其他位置的选择集合。你能告诉我如何一次只访问数组中的一个元素!
答案 0 :(得分:1)
您没有使用正确的语法来访问第二个维度。 [0, 0]
相当于[0]
。逗号不分隔维度,它是简单地计算两个操作数并返回第二个操作数的逗号运算符。多维数组是一个数组数组,因此您可以使用一组单独的括号访问每个维度:arrayname[subscript1][subscript2]
questionbank = [
["Which city is the capital of Pakistan", "Islamabad", "Karachi", "Lahore", "Quetta", "Islamabad"],
["Which city is the capital of U.A.E", "Dubai", "Abu Dhabi", "Sharjah", "Ras Al-Khaimah", "Dubai"],
["Which city is the capital of United States of America", "Austin", "Washington DC", "Boston", "Colorado", "Washington DC"],
["Which city is the capital of UK", "London", "Manchester", "Leeds", "Sunderland", "London"],
["Which city is the capital of Kuwait", "Kuwait City", "Salmiya", "Jahra", "Tokyo", "Kuwait City"],
["Which city is the capital of Saudi Arabia", "Riyadh", "Mecca", "Madinah", "Jeddah", "Riyadh"],
["Which city is the capital of India", "Mumbai", "Delhi", "Bareilly", "Calcutta", "Mumbai"],
["Which city is the capital of Afghanistan", "Kabul", "Kandahar", "Herat", "Jalalabad", "Kabul"],
["Which city is the capital of Ireland", "Dublin", "Belfast", "Cork", "Limerick", "Dublin"]
];
function next_question() {
text = questionbank[0][0];
alert(text);
}
next_question();
答案 1 :(得分:1)
要访问二维数组,您应使用此表示法[0][0]
:
questionbank = [
["Which city is the capital of Pakistan", "Islamabad", "Karachi", "Lahore", "Quetta", "Islamabad"],
["Which city is the capital of U.A.E", "Dubai", "Abu Dhabi", "Sharjah", "Ras Al-Khaimah", "Dubai"],
["Which city is the capital of United States of America", "Austin", "Washington DC", "Boston", "Colorado", "Washington DC"],
["Which city is the capital of UK", "London", "Manchester", "Leeds", "Sunderland", "London"],
["Which city is the capital of Kuwait", "Kuwait City", "Salmiya", "Jahra", "Tokyo", "Kuwait City"],
["Which city is the capital of Saudi Arabia", "Riyadh", "Mecca", "Madinah", "Jeddah", "Riyadh"],
["Which city is the capital of India", "Mumbai", "Delhi", "Bareilly", "Calcutta", "Mumbai"],
["Which city is the capital of Afghanistan", "Kabul", "Kandahar", "Herat", "Jalalabad", "Kabul"],
["Which city is the capital of Ireland", "Dublin", "Belfast", "Cork", "Limerick", "Dublin"]
];
function next_question() {
text = questionbank[0][0];
alert(text);
}
next_question();
答案 2 :(得分:0)
在案例[dimension][anotherDimension]...
中使用标准符号[0][0]
而不是[0,0]
。最后一个在JavaScript方面无效或者至少不能达到你想要的效果。你可能有不同的背景,比如C#,它是一个有效的多维表示法。
答案 3 :(得分:0)
这不是questionbank[0, 0]
,而是questionbank[0][0]
。
答案 4 :(得分:0)
这看起来像是一个二维数组,因此您应该访问:
questionbank[0][0]