无法在 Javascript 中访问我的二维数组
Can't Access my 2-D Array in Javascript
这是一个数组,其中包含索引 0 上的问题和 1-4 上的选项,然后索引 5 上包含答案。我想单独访问这些索引号,但不能。
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();
问题是当我调用 [0,0] 时,数组中的整个句子出现,而不仅仅是数组中的一个元素。我的数组是位置 0 的问题集合和其他位置的选择集合。你能告诉我如何一次只访问数组的一个元素吗!
您没有使用正确的语法来访问第二个维度。 [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();
要访问二维数组,您应该使用以下表示法[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();
使用标准表示法[dimension][anotherDimension]...
在您的情况下[0][0]
而不是[0,0]
.最后一个在 JavaScript 方面是无效的,或者至少不能做你想要的。你可能有不同的背景,如 C#,其中它是一个有效的多维表示法。
这不是
questionbank[0, 0]
而是questionbank[0][0]
。
这看起来像一个 2D 数组,因此您应该访问如下:
questionbank[0][0]
相关文章:
- Epic编辑器可以't访问我的文本区轨道
- 为什么我的HTML似乎无法访问我的javascript
- 如何使用其他 JavaScript 文件中的新类访问我的 createjs 导出的 js 文件
- 无权访问我的控制器 Angular 中的属性.js 1.3
- 无法在 Javascript 中访问我的二维数组
- 在jquery javascript中无法访问我的else
- 在这种情况下,如何访问我的JSON代码
- MVC4剃刀在客户端javascript中访问我的模型中的JsonResult数据
- 如何正确访问我的PHP文件?直接从JS或通过“;主“;PHP
- 如何使用window.open()快速访问我的网站的根目录
- Javascript - 如何检测我的弹出窗口何时访问我的网站
- 如何访问我的捆绑包
- 我无法通过 JavaScript 中的 $().click(calculator.press(“”) 访问我的计算器对象
- 如何防止用户直接访问我的 html 页面
- 其他人如何使用Apache Tomcat 7访问我的本地javascript应用程序(通过我的IP地址)
- 如何在访问者访问我的网站后几秒钟显示 facebook 框模式
- 当使用http访问我的facebook iframe页面时,我如何才能获得Modal而不是弹出窗口
- 检测用户是否正在使用NodeJS访问我的网络(WIFI)
- 为什么我不能从外部访问我的javascript构造函数
- 我可以安全地限制桌面用户访问我的移动网站吗