Javascript 对象内部的对象如何生成随机项?

Javascript 对象内部的对象如何生成随机项?,javascript,Javascript,我有一个对象里面的对象,想要随机生成,是一个问题测验。我试过了 index = Math.floor(Math.random() *currentQuestion.answers[letter]); 运气不好,有人能帮我解释一下为什么我不能随机生成项目吗 我需要在我的createquick函数中生成随机答案项,现在是固定的exp:A:MJ,B:Pippen,C:Magic,如果刷新它将随机生成A:Pippen B:Magic:MJ等等 我的对象变量 const myQuestions = [

我有一个对象里面的对象,想要随机生成,是一个问题测验。我试过了

index = Math.floor(Math.random() *currentQuestion.answers[letter]);
运气不好,有人能帮我解释一下为什么我不能随机生成项目吗

我需要在我的createquick函数中生成随机答案项,现在是固定的exp:A:MJ,B:Pippen,C:Magic,如果刷新它将随机生成A:Pippen B:Magic:MJ等等

我的对象变量

 const myQuestions = [
{
 question: "What's my name ?",
 answers: {
  item1: "Chris",
  item2: "Leborn",
  item3: "Webber"
 },
 correctAnswer: "Chris",
 button: "Next"
},

{
 question: "What's my age ?",
 answers: {
  item1: "31",
  item2: "30",
  item3: "29"
 },
 correctAnswer: "31",
 button: "Next"
},

{
 question: "What's my favor NBA star ?",
 answers: {
  item1: "MJ",
  item2: "Pippen",
  item3: "Magic"
 },
 correctAnswer: "MJ",
 button: "Done"
}
]
功能

function createQuiz() {
  //clear the contents of questions div first 
  document.getElementById('questionsBox').innerHTML = "";

  //clear answers box
  document.getElementById('answersBox').innerHTML = "";


  //set answer sting
  answersCaptcha = [];

  //output
  output = [];

  // for each question...
  myQuestions.forEach(
    (currentQuestion, questionNumber) => {

  // we'll want to store the list of answer choices
  const answers = [];
  const option = ["A","B","C"];
  let count = -1;

  for(letter in currentQuestion.answers){
  count++;

  var index = Math.floor(Math.random() * currentQuestion.answers[letter]);

  // i need to generate random answers item here, now is fixed
  // exp: A: MJ, B:Pippen, C:Magic and if refresh it will randomly generate A:Pippen B:Magic C:MJ and so on.


  answers.push(
  `<label>
  <input type="radio" name="question${questionNumber}" value="${letter}">
  ${option[count]} :
  ${currentQuestion.answers[letter]}
  </label>`
  );
  }


  // add this question and its answers to the output
     output.push(
       `<div class="question"> ${currentQuestion.question} </div>
       <div class="answers"> ${answers.join('')} </div>`
     ); 

  })

  document.getElementById('answersBox').innerHTML = output.join('');
  console.log(answersCaptcha);

}
函数createquick(){
//首先清除问题div的内容
document.getElementById('questionsBox')。innerHTML=“”;
//清除答案框
document.getElementById('answersBox')。innerHTML=“”;
//设置应答器
回答SCAPTCHA=[];
//输出
输出=[];
//对于每个问题。。。
我的问题(
(当前问题,问题编号)=>{
//我们将要存储答案选择列表
常量answers=[];
常量选项=[“A”、“B”、“C”];
让计数=-1;
for(当前问题中的字母。答案){
计数++;
var index=Math.floor(Math.random()*currentQuestion.answers[letter]);
//我需要在这里生成随机答案项,现在已修复
//exp:A:MJ,B:Pippen,C:Magic,如果刷新,它将随机生成A:Pippen,B:Magic:MJ,依此类推。
回答,推(
`
${option[count]}:
${currentQuestion.answers[字母]}
`
);
}
//将此问题及其答案添加到输出中
输出推送(
`${currentQuestion.question}
${answers.join(“”)}`
); 
})
document.getElementById('answersBox')。innerHTML=output.join(“”);
控制台日志(answersCaptcha);
}

如果我正确理解了你的问题。。。你所需要的只是随机排列(currentQuestion.answers)

问题是字母不是数字,不能用它来随机排列。这里有一个解决方案:

const myQuestions=[{
问题:“我叫什么名字?”,
答复:{
项目1:“克里斯”,
第2项:“勒伯恩”,
项目3:“韦伯”
},
正确答案:“克里斯”,
按钮:“下一步”
},
{
问题:“我的年龄是多少?”,
答复:{
项目1:“31”,
项目2:“30”,
项目3:“29”
},
正确答案:“31”,
按钮:“下一步”
},
{
问:“我最喜欢的NBA球星是什么?”,
答复:{
第1项:“MJ”,
第2项:“皮蓬”,
项目3:“魔法”
},
正确答案:“MJ”,
按钮:“完成”
}
]
createquick()
函数createquick(){
//首先清除问题div的内容
document.getElementById('questionsBox')。innerHTML=“”;
//清除答案框
document.getElementById('answersBox')。innerHTML=“”;
//设置应答器
回答SCAPTCHA=[];
//输出
输出=[];
//对于每个问题。。。
我的问题(
(当前问题,问题编号)=>{
//我们将要存储答案选择列表
常量选项=[“A”、“B”、“C”];
让计数=-1;
const answers=shufflarray(Object.entries(currentQuestion.answers)).map((e,i)=>{
返回`
${option[i]}:
${currentQuestion.answers[e[0]]}
`
})
//将此问题及其答案添加到输出中
输出推送(
`${currentQuestion.question}
${answers.join(“”)}`
);
})
document.getElementById('answersBox')。innerHTML=output.join(“”);
控制台日志(answersCaptcha);
}
功能随机播放(arr){
let数组=arr
对于(设i=array.length-1;i>0;i--){
常数j=数学楼层(数学随机()*(i+1));
[array[i],array[j]=[array[j],array[i]];
}
返回数组
}


你能提供你的HTML,这样我们就有了一个更容易测试的工作示例吗?对不起,你介意解释一下这是怎么回事吗?[array[i],array[j]=[array[j],array[i]];我需要理解代码并学习第一课:D,谢谢。代码就是我要找的。这行交换数组中的两个项目(数组[i]和数组[j])。这种语法被称为解构赋值(或仅仅是解构)。更多信息:以前,这种交换需要一个临时变量(和三个步骤),但使用ES6+可以一步完成。