简体   繁体   English

如何从数组中删除随机项,然后将其从数组中删除,直到数组为空

[英]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.

相关问题 从数组中选择随机项并删除它,重新启动一次数组为空 - Select Random Item from Array & Remove It, Restart Once Array is Empty 如何使用数组中的随机元素,从数组中删除该元素,并继续使用随机元素直到数组为空? - How do you use a random element from an array, remove that element from the array, and keep using random elements until the array is empty? 如何 output 随机数组条目 - 然后从数组中删除该项目? - How to output random array entry -- then remove that item from the array afterwards? 从数组中随机删除元素直到为空 - Randomly remove elements from an array until empty 从数组中获取随机项,在for循环中,然后从数组中删除 - Get random item from array, within a for loop, then remove from array 如何从数组中删除空数组值(“”)? - How to remove empty array values ("") from an array? 从 Array 中选择 Random Item(成对显示)并删除它,一旦 Array 为空就重新启动 - Select Random Item (shown in pairs) from Array & Remove It, Restart Once Array is Empty 按顺序从数组中删除项目,直到找到匹配的项目 - Remove items from an array in order until matching item is found 如何删除搜索到的项目并从数组中删除该项目? - how to remove searched item and remove that item from array? 如何从数组 1 中删除项目并将项目推送到数组 2 - How to remove item from array 1 and push item to array 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM