在Javascript中访问索引数组

时间:2013-05-30 16:31:29

标签: javascript

好的,这可能听起来很简单,但这可能在Javascript中。我有像dis

这样的对象(数组)
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 

1 个答案:

答案 0 :(得分: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/