Javascript 将数组值赋给文本

Javascript assign array value to text

谁能告诉我如何将数组值输出到不同的地方。

例如:

var list = [1,2,3]

textbox1 = 2
textbox2 = 1
textbox3 = 3

它的洗牌数组

提前致谢!

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

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

    // 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;
}

var list = [
        1,
        2,
        3
    ]

var shuffle_list = shuffle(list);

text1 = shuffle_list[0]; //2
text2 = shuffle_list[1]; //1
text3 = shuffle_list[2]; //3

试试这个,你可以使用 shuffle 函数来打乱数组。

var original = [1, 2, 3]
var original_length = original.length

var randomShuffle = function(){
    var idx = Math.floor(Math.random() * original.length)
    var number = original[idx]
    original.splice(idx, 1)
    return number
}

for (x = 0; x < original_length; x++){
    var name = "textbox" + (x + 1)
    eval("var " + name + " = " + randomShuffle())
}
console.log(textbox1, textbox2, textbox3)

这将动态生成您的变量,同时从原始数组中随机分配一个值

https://repl.it/CdX5