在Javascript中从数组中删除元素

在Javascript中从数组中删除元素,javascript,Javascript,可能重复: 函数写入(){ 对于(var x=1;x您可以尝试: question.splice(rnd,1) 将其放在循环的末尾,它将删除刚才显示的元素。您可以尝试: question.splice(rnd,1) 将此项放在循环的末尾,它将删除刚刚显示的元素。您可以跟踪已使用的随机索引并避免这些索引,而不是从数组中删除元素。类似于这样: function write() { for (var x = 1; x <= 3; x++) { var question = ne

可能重复:

函数写入(){
对于(var x=1;x您可以尝试:

question.splice(rnd,1)
将其放在循环的末尾,它将删除刚才显示的元素。

您可以尝试:

question.splice(rnd,1)

将此项放在循环的末尾,它将删除刚刚显示的元素。

您可以跟踪已使用的随机索引并避免这些索引,而不是从数组中删除元素。类似于这样:

function write() {
  for (var x = 1; x <= 3; x++) {
    var question = new Array(...);
    var used={}, l=question.length, rnd;
    do {
      rnd = Math.floor(l * Math.random());
    } while (rnd in used);
    used[rnd] = true;
    document.write(question[rnd]);
    document.write("<br>")
  }
}
function write() {
  var question = ["If you are goofy which is your leading foot", "Riding switch is when you do what", "On your toe side which way should you lean", "question 4", "question 5", "question 6"];

  for (var x = 1; x <= 3; x++) {
    var rnd = Math.floor(question.length * Math.random());
    document.write(question.splice(rnd, 1)[0] + "<br>");
  }
}
函数写入(){

对于(var x=1;x,您可以跟踪已经使用过的随机索引并避免它们,而不是从数组中删除元素。类似如下:

function write() {
  for (var x = 1; x <= 3; x++) {
    var question = new Array(...);
    var used={}, l=question.length, rnd;
    do {
      rnd = Math.floor(l * Math.random());
    } while (rnd in used);
    used[rnd] = true;
    document.write(question[rnd]);
    document.write("<br>")
  }
}
function write() {
  var question = ["If you are goofy which is your leading foot", "Riding switch is when you do what", "On your toe side which way should you lean", "question 4", "question 5", "question 6"];

  for (var x = 1; x <= 3; x++) {
    var rnd = Math.floor(question.length * Math.random());
    document.write(question.splice(rnd, 1)[0] + "<br>");
  }
}
函数写入(){

对于(var x=1;x您需要使用数组的方法。但是,您每次迭代都要创建一个新数组,因此您需要将该部分移出循环

function write() {
    var questions = [
        "If you are goofy which is your leading foot",
        "Riding switch is when you do what",
        "On your toe side which way should you lean",
        "question 4",
        "question 5",
        "question 6"
    ];

    for (var x = 1; x <= 3; x++) {
        var rnd = Math.floor(questions.length * Math.random());
        document.write(questions[rnd] + "<br>");
        questions.splice(rnd, 1);
    }
}
函数写入(){
变量问题=[
“如果你是傻瓜,哪只脚是你的前脚”,
“骑行开关是当你做什么”,
“在你的脚趾侧,你应该向哪个方向倾斜”,
“问题4”,
“问题5”,
“问题6”
];

对于(var x=1;x您需要使用数组的方法。但是,您每次迭代都要创建一个新数组,因此您需要将该部分移出循环

function write() {
    var questions = [
        "If you are goofy which is your leading foot",
        "Riding switch is when you do what",
        "On your toe side which way should you lean",
        "question 4",
        "question 5",
        "question 6"
    ];

    for (var x = 1; x <= 3; x++) {
        var rnd = Math.floor(questions.length * Math.random());
        document.write(questions[rnd] + "<br>");
        questions.splice(rnd, 1);
    }
}
函数写入(){
变量问题=[
“如果你是傻瓜,哪只脚是你的前脚”,
“骑行开关是当你做什么”,
“在你的脚趾侧,你应该向哪个方向倾斜”,
“问题4”,
“问题5”,
“问题6”
];

对于(var x=1;x我同意Tim的回答。不过,另外,您可以通过这样做进一步压缩代码:

function write() {
  for (var x = 1; x <= 3; x++) {
    var question = new Array(...);
    var used={}, l=question.length, rnd;
    do {
      rnd = Math.floor(l * Math.random());
    } while (rnd in used);
    used[rnd] = true;
    document.write(question[rnd]);
    document.write("<br>")
  }
}
function write() {
  var question = ["If you are goofy which is your leading foot", "Riding switch is when you do what", "On your toe side which way should you lean", "question 4", "question 5", "question 6"];

  for (var x = 1; x <= 3; x++) {
    var rnd = Math.floor(question.length * Math.random());
    document.write(question.splice(rnd, 1)[0] + "<br>");
  }
}
函数写入(){
var question=[“如果你是傻瓜,哪只脚是你的前脚”,“骑行开关是当你做什么”,“在你的脚趾侧,你应该向哪个方向倾斜”,“问题4”,“问题5”,“问题6”];

对于(var x=1;x我同意Tim的回答。不过,另外,您可以通过这样做进一步压缩代码:

function write() {
  for (var x = 1; x <= 3; x++) {
    var question = new Array(...);
    var used={}, l=question.length, rnd;
    do {
      rnd = Math.floor(l * Math.random());
    } while (rnd in used);
    used[rnd] = true;
    document.write(question[rnd]);
    document.write("<br>")
  }
}
function write() {
  var question = ["If you are goofy which is your leading foot", "Riding switch is when you do what", "On your toe side which way should you lean", "question 4", "question 5", "question 6"];

  for (var x = 1; x <= 3; x++) {
    var rnd = Math.floor(question.length * Math.random());
    document.write(question.splice(rnd, 1)[0] + "<br>");
  }
}
函数写入(){
var question=[“如果你是傻瓜,哪只脚是你的前脚”,“骑行开关是当你做什么”,“在你的脚趾侧,你应该向哪个方向倾斜”,“问题4”,“问题5”,“问题6”];

对于(var x=1;x)如果您非常喜欢编写长行代码,甚至可以将随机数生成移动到拼接函数第一个参数的位置。如果您非常喜欢编写长行代码,甚至可以将随机数生成移动到拼接函数第一个参数的位置。