繁体   English   中英

如何在数组中随机排列数组?

[英]How do I shuffle an array within an array?

设置说明:我正在构建带有微调器的琐事游戏。 该微调器分为6个类别(第6个类别是之前所有5个类别的总和)。 前5个类别将有自己的一组问题。 一旦微调框停在一个类别上,就会出现一个表格,该表格将根据其类别按顺序询问一系列问题。 每个问题有3个选择,其中1个是正确的选择。

下面是一个简短的问题库阵列,以说明我的想法:

var questionBankArray = 
 [{
    category: "Category1",
    question: "What does the following expression return? <br> 3 / 'bob';",
    choices: ["undefined", "ReferenceError", "NaN"],
    correctAnswer: "NaN"
   },{
     category: "Category1"
     question: "What is a method?",
     choices: ["Used to describe an object.", "A function assigned to an object.", "Performs a function on one or more operands or variables."],
     correctAnswer: "A function assigned to an object."
   },{
     category: "Category2"
     question: "Which company first implemented the JavaScript language?",
     choices: ["Netscape Communications Corp.", "Microsoft Corp.", " Sun Microsystems Corp."],
     correctAnswer: "Netscape Communications Corp."
    },{
     category: "Category2"
     question: "When was the first release of a browser supporting JavaScript?",
     choices: ["1996", "1995", " 1994"],
     correctAnswer: "1995"
    },
 ];

```

我想遍历对象的questionBanArray,并按类别在该类别内随机播放。 我还希望能够在该类别的每个问题中随机选择。 我将如何处理? 将其重写为如下所示会更容易吗:

questionBankArray = 
[{
  CategoryBank1: 
    [{
        question1: "What is blank?", 
        choices: ["choice1","choice2","answer"], 
        answer: "answer"
        },{
        question2: "What is blank?", 
        choices: ["choice1","choice2","answer"], 
        answer: "answer"
     }],
   CategoryBank2: 
     [{
        question1: "What is blank?", 
        choices: ["choice1","choice2","answer"], 
        answer: "answer"
        },{
        question2: "What is blank?", 
        choices: ["choice1","choice2","answer"], 
        answer: "answer"
      }]
 }];

我认为理想的结构应该是这样的:

questionBankArray = 
  [{
    category:"first category",
    questions: 
      [{
        question1: "What is blank?", 
        choices: ["choice1","choice2","answer"], 
        answer: "answer"
      },{
        question2: "What is blank?", 
        choices: ["choice1","choice2","answer"], 
        answer: "answer"
      }]
  },
  {
    category: "second category",
    questions:
      [{
        question1: "What is blank?", 
        choices: ["choice1","choice2","answer"], 
        answer: "answer"
      },{
        question2: "What is blank?", 
        choices: ["choice1","choice2","answer"], 
        answer: "answer"
      }]
    }];

创建一个随机播放功能:

function shuffle(o){
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
}

开始循环遍历外部数组,并从最内部的层次到外部数组应用随机播放功能,从而越来越深入

for (var category in questionBankArray) {
  for (var question in category.questions) {
    shuffle(question.choices);
  }
  shuffle(category);
}
shuffle(questionBankArray);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM