无法在 Javascript 中访问我的二维数组

Can't Access my 2-D Array in Javascript

本文关键字:访问 我的 二维数组 Javascript      更新时间:2023-09-26

这是一个数组,其中包含索引 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]