2013-05-30 81 views
-3

好吧,這聽起來很簡單,但這可能在JavaScript中。我有一個對象(陣列),如DIS在JavaScript中訪問索引數組

questions{ 
0: 
{A:"A in index 0", 
B:"B in index 0", 
C:"C in index 0" 
}, 
1: 
{A:"A in index 1", 
B:"B in index 1", 
C:"C in index 1" 
}, 
2: 
{A:"A in index 2", 
    B:"B in index 2", 
    C:"C in index 2" 
} 
} 

我怎麼做這樣的事情

questions[0].A; //output : A in index 0 
questions[2].B; //output : B in index 2 
+0

這是否不行? – JJJ

+2

不應該問''''問題:{'或'questions = {' –

+0

不,它給我一個未定義的錯誤 – kolexinfos

回答

3

在這裏你去。你基本上需要將它分配給問題。

var questions = { 
    0: { 
     A: "A in index 0", 
     B: "B in index 0", 
     C: "C in index 0" 
    }, 
    1: { 
     A: "A in index 1", 
     B: "B in index 1", 
     C: "C in index 1" 
    }, 
    2: { 
     A: "A in index 2", 
     B: "B in index 2", 
     C: "C in index 2" 
    } 
} 
console.log(questions[0].A); //output : A in index 0 
console.log(questions[2].B); //output : B in index 2 

的jsfiddle:http://jsfiddle.net/basarat/8jVHg/