[英]How to remove random item from array and then remove it from array until array is empty
I am trying to remove a random item from an array until the array is empty using jquery or javascript. 我试图从数组中删除一个随机项,直到数组为空,使用jquery或javascript。 I need to console out each time the random item.
每次随机项都需要控制台。 Basically i am going to create an element with a random image from the given array until all images have been created.
基本上我将使用给定数组中的随机图像创建一个元素,直到所有图像都已创建。
Here is my attempt for getting the random item and removing from array but it it does not go through the entire array-I am stumped. 这是我尝试获取随机项并从数组中删除,但它没有通过整个数组 - 我很难过。
"load": function(){
var imgArray = ['brain', 'mitochondria', 'microsope', 'beaker', 'beaker-2', 'scientist', 'cell', 'atom', 'dropper'];
function randomItem(array){
var arrayLength = array.length+1;
console.log(arrayLength);
for(var i = 0;i<array.length;i++){
var item = array[Math.floor(Math.random()*array.length)];
array.pop(item);
console.log(array);
}
}
randomItem(imgArray);
},
Here is my console output: 这是我的控制台输出:
10
home.js:12 ["brain", "mitochondria", "microsope", "beaker", "beaker-2", "scientist", "cell", "atom"]
home.js:12 ["brain", "mitochondria", "microsope", "beaker", "beaker-2", "scientist", "cell"]
home.js:12 ["brain", "mitochondria", "microsope", "beaker", "beaker-2", "scientist"]
home.js:12 ["brain", "mitochondria", "microsope", "beaker", "beaker-2"]
home.js:12 ["brain", "mitochondria", "microsope", "beaker"]
The function, Array.prototype.pop()
will remove an element from the last. 函数
Array.prototype.pop()
将从最后一个元素中删除一个元素。 So at this context, you have to use Array.prototype.splice(indext,cnt)
, 所以在这种情况下,你必须使用
Array.prototype.splice(indext,cnt)
,
for(var i = array.length-1;i>=0;i--){
array.splice(Math.floor(Math.random()*array.length), 1);
console.log(array);
}
And since we are altering the array, we have to traverse it in reverse way, so that the index will not be collapsed. 因为我们正在改变数组,所以我们必须以相反的方式遍历它,这样索引就不会崩溃。
Just make a random index and splice while length is greater than zero. 只要在长度大于零的情况下进行随机索引和拼接。
var data = ["brain", "mitochondria", "microsope", "beaker", "beaker-2", "scientist", "cell", "atom"]; while (data.length) { document.write(data.splice(data.length * Math.random() | 0, 1)[0] + '<br>'); }
Array.prototype.pop removes the last element from the array, not a specific element. Array.prototype.pop从数组中删除最后一个元素,而不是特定元素。 To remove an element at a specific index you can use Array.prototype.splice (See: How do I remove a particular element from an array in JavaScript? ).
要删除特定索引处的元素,可以使用Array.prototype.splice (请参阅: 如何从JavaScript中删除数组中的特定元素? )。
You also have a problem with for(var i = 0;i<array.length;i++)
, since array.length
changes whenever you remove an item, you'll only get through half the array, you can either loop in reverse for ( var i = array.length; i--; )
, so that array.length
is only evaluated once, before the first iteration, or you can use a while loop while( array.length )
. 你也有一个问题,
for(var i = 0;i<array.length;i++)
因为array.length
变化当你删除一个项目,你只完成了一半的阵列得到,你可以在任何反向循环for ( var i = array.length; i--; )
,因此array.length
仅在第一次迭代之前计算一次,或者您可以使用while循环while( array.length )
。
Change your loop to: 将你的循环改为:
while( array.length ) {
var index = Math.floor( Math.random()*array.length );
console.log( array[index] ); // Log the item
array.splice( index, 1 ); // Remove the item from the array
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.