Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 洗牌对象数组,但不多次拾取同一项_Javascript_Arrays_Json - Fatal编程技术网

Javascript 洗牌对象数组,但不多次拾取同一项

Javascript 洗牌对象数组,但不多次拾取同一项,javascript,arrays,json,Javascript,Arrays,Json,我有一系列的问题,我想随机化,并把它们放到测验中。然而,目前它可以选择同一个问题两次,并把它放在测验两次。如果我想在问题被推送到一个新数组后删除它,而不知道它的索引,我如何删除它 我试过的代码是这样的 var index = questionsArrayCopy.findIndex(function(item, i){return item.question === randomQuestion}); questionsArrayCopy.splice(index) 但它不起作用,我也没有错误

我有一系列的问题,我想随机化,并把它们放到测验中。然而,目前它可以选择同一个问题两次,并把它放在测验两次。如果我想在问题被推送到一个新数组后删除它,而不知道它的索引,我如何删除它

我试过的代码是这样的

var index = questionsArrayCopy.findIndex(function(item, i){return item.question === randomQuestion});
questionsArrayCopy.splice(index)
但它不起作用,我也没有错误

我目前的代码如下:

const myQuestions = [
  {
    question: 'What is 37 x 89?',
    answers: [
      { text: '6989', correct: false },
      { text: '3293', correct: true },
      { text: '2400', correct: false },
      { text: '9870', correct: false }
    ]
  },
  {
    question: 'What year is the great fire of london?',
    answers: [
      { text: '1666', correct: true },
      { text: '1888', correct: false },
      { text: '1600', correct: false },
      { text: '1566', correct: false }
    ]
  },
  {
    question: 'Is web development fun?',
    answers: [
      { text: 'Kinda', correct: false },
      { text: 'YES!!!', correct: true },
      { text: 'Um no', correct: false },
      { text: 'IDK', correct: false }
    ]
  },
  {
    question: 'What is 4 * 2?',
    answers: [
      { text: '6', correct: false },
      { text: '8', correct: true }
    ]
  },
]

function getRandomNumberBetween(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

const GetRandomQuestionFromArray = (array) => {
   const randomIndex = getRandomNumberBetween(0, array.length);
   return array[randomIndex];
}

const GetRandomQuestionsArray = (howManyQuestions) => {
  const questionsArrayCopy = [...myQuestions];
  const finalQuestionArray = [];

  for (let i = 0; i < howManyQuestions; i++) {
      const randomQuestion = GetRandomQuestionFromArray(questionsArrayCopy);
      finalQuestionArray.push(randomQuestion);
  }
  return finalQuestionArray;
}

const questions = GetRandomQuestionsArray(3);
console.log(questions)
const myQuestions=[
{
问题:“37 x 89是多少?”,
答复:[
{text:'6989',correct:false},
{text:'3293',correct:true},
{文本:“2400”,正确:false},
{文本:“9870”,正确:false}
]
},
{
问题:“伦敦大火是哪一年?”,
答复:[
{text:'1666',correct:true},
{text:'1888',correct:false},
{text:'1600',correct:false},
{文本:“1566”,正确:false}
]
},
{
问题:“web开发有趣吗?”,
答复:[
{text:'Kinda',correct:false},
{文本:“是!!!”,正确:true},
{text:'Um no',correct:false},
{text:'IDK',correct:false}
]
},
{
问题:“什么是4*2?”,
答复:[
{text:'6',correct:false},
{文本:“8”,正确:true}
]
},
]
函数getRandomNumberBetween(最小值、最大值){
min=数学单元(min);
最大值=数学楼层(最大值);
返回Math.floor(Math.random()*(max-min+1))+min;
}
常量GetRandomQuestionFromArray=(数组)=>{
const randomIndex=getRandomNumberBetween(0,array.length);
返回数组[随机索引];
}
常量GetRandomQuestionsArray=(有多少个问题)=>{
const questionsArrayCopy=[…我的问题];
常量finalQuestionArray=[];
for(设i=0;i

如果我想阻止重复的问题进入最后一个数组(问题),而只是从另一个数组(myQuestions)中随机选择一个问题,我该怎么做?

在array java脚本中使用filter mentod

在数组中使用

const result = myQuestions.filter(el => el.question !=='What is 4 * 2?');

console.log(result)

使用
while
循环和inside,仅当问题尚未出现时才将其添加到最终数组中

使用
find
方法检查问题是否已经存在

无需复制阵列

const myQuestions=[
{
问题:“37 x 89是多少?”,
答复:[
{text:'6989',correct:false},
{text:'3293',correct:true},
{文本:“2400”,正确:false},
{文本:“9870”,正确:false}
]
},
{
问题:“伦敦大火是哪一年?”,
答复:[
{text:'1666',correct:true},
{text:'1888',correct:false},
{text:'1600',correct:false},
{文本:“1566”,正确:false}
]
},
{
问题:“web开发有趣吗?”,
答复:[
{text:'Kinda',correct:false},
{文本:“是!!!”,正确:true},
{text:'Um no',correct:false},
{text:'IDK',correct:false}
]
},
{
问题:“什么是4*2?”,
答复:[
{text:'6',correct:false},
{文本:“8”,正确:true}
]
},
]
函数getRandomNumberBetween(最小值、最大值){
min=数学单元(min);
最大值=数学楼层(最大值);
返回Math.floor(Math.random()*(max-min+1))+min;
}
常量getRandomQuestionFromArray=(数组)=>{
const randomIndex=getRandomNumberBetween(0,array.length-1);
返回数组[随机索引];
}
常量getRandomQuestionsArray=(有多少个问题)=>{
常量finalQuestionArray=[];
//继续,直到达到要求的问题数
while(finalQuestionArray.lengthq.question===randomQuestion.question)){
finalQuestionArray.push(随机问题);
}
}
返回最终问题数组;
}
常量问题=getRandomQuestionsArray(3);

控制台日志(问题)您已经有了所选项目的索引,您只是将其扔掉了:

const GetRandomQuestionFromArray = (array) => {
   const randomIndex = getRandomNumberBetween(0, array.length); //<-- here it is!!
   return array[randomIndex];
}
实例:

const myQuestions=[
{
问题:“37 x 89是多少?”,
答复:[
{text:'6989',correct:false},
{text:'3293',correct:true},
{文本:“2400”,正确:false},
{文本:“9870”,正确:false}
]
},
{
问题:“伦敦大火是哪一年?”,
答复:[
{text:'1666',correct:true},
{text:'1888',correct:false},
{text:'1600',correct:false},
{文本:“1566”,正确:false}
]
},
{
问题:“web开发有趣吗?”,
答复:[
{text:'Kinda',correct:false},
{文本:“是!!!”,正确:true},
{text:'Um no',correct:false},
{text:'IDK',correct:false}
]
},
{
问题:“什么是4*2?”,
答复:[
{text:'6',correct:false},
{文本:“8”,正确:true}
]
},
]
函数getRandomNumberBetween(最小值、最大值){
返回Math.floor(Math.random()*(max-min))+min;
}
常量GetRandomQuestionsArray=(有多少个问题)=>{
const questionsArrayCopy=[…我的问题];
常量finalQuestionArray=[];
for(设i=0;iconsole.log(questions)
与其随机挑选一个问题,为什么不随机洗牌问题数组,然后选择<
const GetRandomQuestionFromArray = (array) => {
   const randomIndex = getRandomNumberBetween(0, array.length); //<-- here it is!!
   return array[randomIndex];
}
const GetRandomQuestionsArray = (howManyQuestions) => {
  const questionsArrayCopy = [...myQuestions];
  const finalQuestionArray = [];

  for (let i = 0; i < howManyQuestions; i++) {
      const randomIndex = getRandomNumberBetween(0, questionsArrayCopy .length);
    
      const randomQuestion = questionsArrayCopy.splice(randomIndex,1);
      finalQuestionArray.push(randomQuestion[0]);
  }
  return finalQuestionArray;
}