任何人都可以告诉我如何将数组值输出到不同的地方。
例如:
var list = [1,2,3]
textbox1 = 2
textbox2 = 1
textbox3 = 3
它的shuffle数组
提前谢谢你!
答案 0 :(得分:1)
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函数。
答案 1 :(得分:0)
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)
这将动态生成变量,同时从原始数组中随机分配一个值