【问题标题】:How to remove random item from array and then remove it from array until array is empty如何从数组中删除随机项,然后将其从数组中删除,直到数组为空
【发布时间】:2016-07-04 08:25:45
【问题描述】:

我正在尝试使用 jquery 或 javascript 从数组中删除一个随机项,直到数组为空。每次随机项目我都需要安慰。 基本上,我将使用给定数组中的随机图像创建一个元素,直到创建所有图像为止。

这是我获取随机项并从数组中删除的尝试,但它并没有遍历整个数组——我很难过。

"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);
},

这是我的控制台输出:

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"]

【问题讨论】:

    标签: javascript jquery arrays for-loop


    【解决方案1】:

    函数Array.prototype.pop() 将从最后一个元素中删除一个元素。所以在这种情况下,你必须使用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);
    }
    

    而且由于我们正在改变数组,所以我们必须以相反的方式遍历它,这样索引就不会被折叠。

    【讨论】:

    • 非常感谢!肯定会更深入地研究有关数组的文档。
    • @TravisMichaelHeller 很高兴为您提供帮助! :)
    【解决方案2】:

    只要在长度大于零的情况下做一个随机索引和拼接。

    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>');
    }

    【讨论】:

    • 感谢您的其他选择。我没有考虑过使用 while 循环,因为现在我想到了它,这很有意义。
    【解决方案3】:

    Array.prototype.pop 从数组中删除最后一个元素,而不是特定元素。要删除特定索引处的元素,您可以使用Array.prototype.splice(参见:How do I remove a particular element from an array in JavaScript?)。

    for(var i = 0;i&lt;array.length;i++) 也有问题,因为每当你删除一个项目时array.length 会发生变化,你只会通过一半的数组,你可以反向循环for ( var i = array.length; i--; ),所以array.length 是仅在第一次迭代之前评估一次,或者您可以使用 while 循环 while( array.length )

    将循环更改为:

    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
    }
    

    【讨论】:

    • 感谢您抽出宝贵时间提供帮助。我只是告诉 Nina,对于我正在寻找的功能来说,while 循环会是更好的选择。
    • 刚看到你的更新,谢谢你的解释。从来没有想过反向循环。有一段时间不用搞乱数组了,但我显然需要读回它们,因为我缺乏知识。
    • @TravisMichaelHeller 您也可以像以前那样循环前进,但先将数组长度放入变量中:var numItems = array.length; 然后for(var i = 0;i&lt;numItems;i++){,因为array.length 在您删除元素时会发生变化,但变量不会受到影响。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    相关资源
    最近更新 更多