2016-04-16 3 views
1

Dies ist ein Array, das die Frage zu Index 0 und Auswahlmöglichkeiten von 1-4 enthält und dann auf Index 5 die Antwort enthält. Ich möchte auf diese Indexnummern einzeln zugreifen, aber nicht.Zugriff auf mein 2-D-Array in Javascript

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();

Das Problem ist, wenn ich [0,0] in der Anordnung der ganze Satz aufrufen, anstatt nur ein Element in dem Array kommt. Mein Array ist eine Sammlung von Fragen zu Position 0 und der Sammlung von Auswahlmöglichkeiten in den anderen Positionen. Kannst du mir bitte sagen, wie man immer nur auf ein Element des Arrays zugreifen kann?

Antwort

1

Sie verwenden nicht die korrekte Syntax für den Zugriff auf die zweite Dimension. [0, 0] entspricht [0]. Das Komma trennt keine Dimensionen, es ist der Komma-Operator, der einfach beide Operanden auswertet und den zweiten zurückgibt. Ein mehrdimensionales Array ist ein Array von Arrays, so greifen Sie jede Dimension mit einem separaten Satz von Klammern: 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

Dank! Du bist großartig :) –

+0

Schöne Erklärung über den Komma-Operator. –

1

Für den Zugriff auf zweidimensionale Arrays sollten Sie diese Schreibweise verwenden [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();

+0

Danke! Du bist großartig :) –

0

Verwenden Sie die Standardnotation [dimension][anotherDimension]... in Ihrem Fall [0][0] anstelle von [0,0]. Der letzte ist nicht in Bezug auf JavaScript gültig oder tut zumindest nicht, was Sie wollen. Sie könnten einen anderen Hintergrund haben, wie C#, wo es eine gültige mehrdimensionale Notation ist.

0

Dies ist nicht questionbank[0, 0] aber questionbank[0][0].

+0

Danke! du bist unglaublich :) –

0

Das sieht aus wie eine 2D-Array, so dass Sie wie zugreifen sollten:

questionbank[0][0] 
Verwandte Themen