Javascript 如果已经拾取了值,如何让JS拾取不同的值?

Javascript 如果已经拾取了值,如何让JS拾取不同的值?,javascript,jquery,Javascript,Jquery,我有一个问题,我不能让JS一直打印出3个不同的值,它有时会选择两个不同的值。有人能帮忙吗 var receivedArray = JSON.parse('<?php echo json_encode($unserialize);?>'); const random = receivedArray; const correctAnswer = random[Math.floor(Math.random() * random.length)]; const guess1 = random

我有一个问题,我不能让JS一直打印出3个不同的值,它有时会选择两个不同的值。有人能帮忙吗

var receivedArray = JSON.parse('<?php echo json_encode($unserialize);?>');
const random = receivedArray;
const correctAnswer = random[Math.floor(Math.random() * random.length)];
const guess1 = random[Math.floor(Math.random() * random.length)];
const guess2 = random[Math.floor(Math.random() * random.length)];


$(document).ready(function() {
  $("#test-div").append(
    "<div class=\"row\">\n" +
    "<div class=\"col-6\">\n" +
    "<img id=\"testImage\" src=\"\" alt='...' height=\"540px\"/>\n" +
    "</div>\n" +
    "<div class=\"col-6\">\n" +
    "<h4 class=\"Guess\" id=\"Guess\">ATMINI JŪRNIEKU MEZGLA NOSAUKUMU</h4>\n" +
    "<div id=\"shuffle\">" +
    "<div class=\"btn guesses\" >" + correctAnswer.nameLV + "</div><br>" +
    "<div class=\"btn guesses\" >" + guess1.nameLV + "</div><br>" +
    "<div class=\"btn guesses\" >" + guess2.nameLV + "</div>" +
    "</div>\n" +
    "</div>\n" +
    "</div>\n"

  );
  $("#testImage").attr("src", "../Images/uploads/" + correctAnswer.Image);
  console.log("BS")
});
var receivedArray=JSON.parse(“”);
常数随机=接收数据;
const correctAnswer=random[Math.floor(Math.random()*random.length)];
const guess1=random[Math.floor(Math.random()*random.length)];
const guess2=random[Math.floor(Math.random()*random.length)];
$(文档).ready(函数(){
$(“#测试div”).append(
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“ATMINI JŪRNIEKU MEZGLA Nosakumu\n”+
"" +
“+correctAnswer.nameLV+”
“+ “+guess1.nameLV+”
“+ “”+guess2.nameLV+“”+ “\n”+ “\n”+ “\n” ); $(“#testImage”).attr(“src”,”./Images/uploads/“+correctAnswer.Image); 控制台日志(“BS”) });
您应该使用从
随机
数组中删除项目,只要选择了答案:

const correctAnswer = random.splice(Math.floor(Math.random() * random.length), 1);
const guess1 = random.splice(Math.floor(Math.random() * random.length), 1);
const guess2 = random.splice(Math.floor(Math.random() * random.length), 1);

是的,如果它只是从列表中多次随机选择一个项目,它有时会随机选择同一个项目,这是有道理的

取而代之的是,一旦项目被随机选择,就可以将其删除。如果以后需要访问原始阵列,可以先创建阵列的副本

const random = [...receivedArray]; //copy
const correctAnswer= random.splice(Math.floor(Math.random() * random.length),1)[0];
const guess1 = random.splice(Math.floor(Math.random() * random.length),1)[0];
const guess2 = random.splice(Math.floor(Math.random() * random.length),1)[0];
Splice删除并返回指定数量的项。你看到的第二个参数是要删除的数字,在你的例子中是1。Splice返回一个数组(即使是一个项),因此您必须添加[0]才能访问该项本身


由于该项目将被删除,因此无法再次猜测!简单

洗牌一个数组,然后选择前三个值

const [correctAnswer, guess1, guess2] = shuffle(random);
采取或使用

提供了一个简单易读的洗牌函数。只需使用
randoSequence
函数洗牌,即可获得您的值

var数组=[“一”、“二”、“三”];
var shuffled=随机序列(数组);
log(shuffled.pop().value,shuffled.pop().value)

我是重写已经定义的随机值还是将其放入if语句中?那么它检查了吗?
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}