簡體   English   中英

無法使用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 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(); 

問題是當我調用[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 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(); 

要訪問二維數組,您應該使用以下符號[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(); 

在您的情況下,使用標准符號[dimension][anotherDimension]... [0][0]代替[0,0] 最后一個在JavaScript方面無效,或者至少不能滿足您的要求。 您可能有不同的背景,例如C#,它是有效的多維表示法。

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

這看起來像一個二維數組,因此您應該像這樣訪問:

questionbank[0][0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM