【问题标题】:Best data structure for multiple choice quiz in JavaScriptJavaScript中多项选择测验的最佳数据结构
【发布时间】:2012-05-19 14:21:59
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    应该这样做:

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

    【讨论】:

      【解决方案2】:
      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
          ]
      }
      

      【讨论】:

      • 不是真的;这几乎是一样的。
      猜你喜欢
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2020-12-05
      • 2021-03-22
      相关资源
      最近更新 更多