如何弹出从阵列中选择的随机数?
var number = ["a", "b", "c"];
var random = number[Math.floor(Math.random()*number.length)];
答案 0 :(得分:2)
使用splice
var number = ["a", "b", "c"];
var random = Math.floor(Math.random()*number.length);
console.log(number[random], ' is chosen');
var taken = number.splice(random, 1);
console.log('after removed, ', number);
console.log('number taken, ', taken);

答案 1 :(得分:2)
使用splice和随机数作为索引。
number.splice(random, 1);
答案 2 :(得分:1)
您可以使用Splice从数组中删除一定数量的项目。此方法将对原始数组进行处理,并返回已删除的值。
Splice方法中的第一个参数是起点。第二个参数是要删除的项目数。
示例:强>
// 0 1 2
var array = ["a", "b", "c"];
var splicedItem = array.splice(1,1);
// The array variable now equals ["a", "c"]
console.log("New Array Value: " + array);
// And the method returned the removed item "b"
console.log("Spliced Item: " + splicedItem);

您还可以在第一个参数中使用负数开始从数组末尾向后计数。
示例:强>
// -6 -5 -4 -3 -2 -1
var array2 = ["a", "b", "c", "d", "e", "f"];
var splicedItem2 = array2.splice(-3, 2);
// The array2 variable now equals ["a", "b", "c", "f"]
console.log("New Array Value: " + array2);
// The values removed were "d" and "e"
console.log("Spliced Item: " + splicedItem2);

您甚至可以通过包含其他参数将新项目插入到数组中。如果您不想,也不需要将拼接的项目返回到新的变量。
示例:强>
var array3 = ["a", "b", "c", "d", "e", "f"];
array3.splice(2, 2, "Pink", "Mangoes");
// The array3 value is now ["a", "b", "Pink", "Mangoes", "e", "f"]
console.log("New Array Value: " + array3);