我是JavaScript新手,我想从我的数组中获取三个随机元素,这些元素需要不同。我已经设法得到三个不同的元素,然后我尝试通过split方法只获取一个元素。但显然这并没有真正起作用,可能会有很多错误,因为这是我的第一个脚本之一。它有时会说" undefined"。
HTML:
<span id="tot1"></span>, <span id="tot2"> und </span> und <span id="tot3"></span>
使用Javascript:
function getNumber() {
random = Math.floor(Math.random() * students.length);
students.splice(random,1);
return random;
}
students = new Array("Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard",
"Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua")
getNumber();
tot1 = students[random];
getNumber();
tot2 = students[random];
getNumber();
tot3 = students[random];
document.getElementById('tot1').innerHTML = tot1;
document.getElementById('tot2').innerHTML = tot2;
document.getElementById('tot3').innerHTML = tot3;
答案 0 :(得分:7)
通过使用.splice()
,在检索之前实际删除了随机选择的名称,使random
改为引用集合中的下一个名称:
var students = [ "Paul", "Jan", "Fabian D." ];
var random = 1;
console.log(students[random]); // "Jan"
students.splice(random, 1);
console.log(students); // [ "Paul", "Fabian D." ]
console.log(students[random]); // "Fabian D."
&#34;它有时会说&#39; undefined&#39;&#34;当random
尝试引用最后一个元素时发生:
var students = [ "Paul", "Jan", "Fabian D." ];
var random = 2;
console.log(students[random]); // "Fabian D."
students.splice(random, 1);
console.log(students); // [ "Paul", "Jan" ]
console.log(students[random]); // undefined, random is now out-of-bounds at 2
您可以使用.splice()
的返回值,它是已删除元素的集合,重新定义getNumber()
而不是返回元素而不是索引:
function getStudent() {
var random = Math.floor(Math.random() * students.length);
return students.splice(random, 1)[0]; // return the 1st and only removed element
}
var tot1 = getStudent(); // "Fabian D."
var tot2 = getStudent(); // "Enes"
var tot3 = getStudent(); // "Joyce"
答案 1 :(得分:5)
我认为这将是一个更好,更清洁,更短的解决方案:
function getStudent(students) {
var random = Math.floor(Math.random() * (students.length));
var my_student = students.splice(random,1);
return my_student;
}
var students = new Array("Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard",
"Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua");
document.getElementById('tot1').innerHTML = getStudent(students);
document.getElementById('tot2').innerHTML = getStudent(students);
document.getElementById('tot3').innerHTML = getStudent(students);
您将当前学生数组传递给getStudent()
函数,以避免在脚本中使用全局变量。
splice
返回删除的元素,因此您在那里获得了学生姓名并将其返回。
edit2 :我对-1
上的.length
错了。 1
是Math.random()
的独家结果,所以你一直都是对的。
http://jsfiddle.net/northkildonan/38rf42f7/1/
编辑:此方法完全避免在脚本中使用全局变量。
答案 2 :(得分:3)
试试这个:
students = new Array("Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard", "Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua")
var arr = []
while(arr.length < 3){
var randomnumber=Math.ceil(Math.random()*students.length-1)
var found=false;
for(var i=0;i<arr.length;i++){
if(arr[i]==randomnumber){found=true;break}
}
if(!found)arr[arr.length]=randomnumber;
}
console.log(arr.valueOf());
tot1 = students[arr[0]];
tot2 = students[arr[1]];
tot3 = students[arr[2]];
document.getElementById('tot1').innerHTML = tot1;
document.getElementById('tot2').innerHTML = tot2;
document.getElementById('tot3').innerHTML = tot3;
请注意,长度应该减1,因为索引从0开始。
答案 3 :(得分:1)
另一种选择是将整个学生列表洗牌,然后从洗牌组中选择前三个。 (此处的随机播放功能改编自https://stackoverflow.com/a/6274398/567595。)
function shuffle(array) {
var i, temp, j;
for (i = array.length; i; i--) {
j = Math.floor(Math.random() * i);
temp = array[i - 1];
array[i - 1] = array[j];
array[j] = temp;
}
}
students = ["Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard",
"Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
"Enes", "Leon", "Boran", "Joshua"];
shuffle(students);
document.getElementById('tot1').innerHTML = students[0];
document.getElementById('tot2').innerHTML = students[1];
document.getElementById('tot3').innerHTML = students[2];