3

我目前有这个数据集用于测验应用程序(它有多糟糕?):

   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 - 这是一个不错的选择吗?

4

2 回答 2

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}
];
于 2012-05-11T00:05:33.837 回答
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
    ]
}
于 2012-05-11T00:02:15.633 回答