JavaScript中多项选择测验的最佳数据结构

时间:2012-05-10 23:58:49

标签: javascript arrays object data-structures multidimensional-array

我目前正在使用此数据集进行测验应用程序(有多糟糕?):

   var questions =
        [
        'Question 1 text', 
        'Question 2 text'
        ]

    var answers =
        // Question 0
        [
            ['Choice 0', // Right answer
            'Choice 1',
            'Choice 2']
        ]
        // Question 1
        [
            ['Choice 0',
            'Choice 1', // Right answer
            'Choice 2']
        ]

    var correctChoice = 
        [
        '0', // Question 0, choice 0
        '1' //  Question 1, choice 1
        ]

所以我依靠“隐形指数”把它们连在一起,这有点难以维持。 有没有更好的方法来解决这个问题?对象更好吗?一般的建议或最佳做法?

我一直在考虑JSON - 这会是一个不错的选择吗?

2 个答案:

答案 0 :(得分:9)

这样的事情应该做:

var quiz = [
  {question: 'Question 1 text', answers: ['answer 1', 'answer 2'], correct: 0},
  {question: 'Question 2 text', answers: ['answer 1', 'answer 2'], correct: 1}
];

答案 1 :(得分:1)

var quiz = {
    questions: [
        'Question 1 text', 
        'Question 2 text'
    ],
    answers: [
        [//Question 0
            'Choice 0', // Right answer
            'Choice 1',
            'Choice 2'
        ],
        [//Question 1
            'Choice 0',
            'Choice 1', // Right answer
            'Choice 2'
        ]
    ],
    correctChoice: [
        '0', // Question 0, choice 0
        '1' //  Question 1, choice 1
    ]
}
相关问题