简体   繁体   English

无法使用Javascript访问我的二维数组

[英]Can't Access my 2-D Array in Javascript

this is an array that contains the question on index 0 and choices from 1-4 and then on index 5 contains the answer. 这是一个数组,其中包含有关索引0的问题以及从1-4中选择的问题,然后包含索引5的问题。 I want to access these index numbers individually but cant. 我想单独访问这些索引号,但不能。

 questionbank = [ ["Which city is the capital of Pakistan", "Islamabad", "Karachi", "Lahore", "Quetta", "Islamabad"], ["Which city is the capital of UAE", "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(); 

The problem is when i call [0,0] the whole sentence in the array comes up instead of just one element in the array. 问题是当我调用[0,0]时,出现数组中的整个句子,而不仅仅是数组中的一个元素。 My array is a collection of questions on position 0 and the collection of choices in the other positions. 我的数组是关于职位0的问题的集合,以及其他职位的选择的集合。 Can you please tell me how to access only one element of the array at a time! 您能告诉我如何一次只访问数组的一个元素吗?

You're not using the correct syntax to access the second dimension. 您没有使用正确的语法访问第二维。 [0, 0] is equivalent to [0] . [0, 0]等效于[0] The comma doesn't separate dimensions, it's the comma operator that simply evaluates both operands and returns the second one. 逗号不分隔维,它是逗号运算符,它简单地计算两个操作数并返回第二个操作数。 A multi-dimensional array is an array of arrays, so you access each dimension with a separate set of brackets: arrayname[subscript1][subscript2] 多维数组是数组的数组,因此您可以使用一组单独的括号访问每个维度: arrayname[subscript1][subscript2]

 questionbank = [ ["Which city is the capital of Pakistan", "Islamabad", "Karachi", "Lahore", "Quetta", "Islamabad"], ["Which city is the capital of UAE", "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(); 

To access to two-dimensional array you should use this notation [0][0] : 要访问二维数组,您应该使用以下符号[0][0]

 questionbank = [ ["Which city is the capital of Pakistan", "Islamabad", "Karachi", "Lahore", "Quetta", "Islamabad"], ["Which city is the capital of UAE", "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(); 

Use the standard notation [dimension][anotherDimension]... in your case [0][0] instead of [0,0] . 在您的情况下,使用标准符号[dimension][anotherDimension]... [0][0]代替[0,0] The last one is not valid in terms of JavaScript or at least does not do what you want. 最后一个在JavaScript方面无效,或者至少不能满足您的要求。 You might have a different background, like C#, where it is a valid multidimensional notation. 您可能有不同的背景,例如C#,它是有效的多维表示法。

这不是questionbank[0, 0]而是questionbank[0][0]

这看起来像一个二维数组,因此您应该像这样访问:

questionbank[0][0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM