gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:08:23 24 4
gpt4 key购买 nike

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

关于javascript - 无法在 Javascript 中访问我的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36670270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com