简体   繁体   English

使用splice方法无限循环JavaScript遍历数组

[英]Looping through array using splice method infinite loop javascript

When i loop through the array using the splice method, the page just freezes. 当我使用splice方法遍历数组时,页面冻结。 It looks like i caused an infinite loop. 看来我造成了无限循环。 lib.randomInt() works, so that is not the problem. lib.randomInt()有效,所以这不是问题。

function() {
return function(string) {
    var arr = string.split("")
    arr.sort();
    for(var i = 0; arr.length;i++){
        arr.splice((i+1),0,lib.randomInt(9));
    }
    var pseudocryptarr = arr.join("");
}
})()("example");

This is from a different file that is placed above the main file in html 这来自放置在html中主文件上方的其他文件

var lib = {
factorial: function(num){
  function _factorial(num){
    if(num === 1){
        return 1;
        } else {
            return num*_factorial(num-1);
        }
    }
    console.log(num+"! = " + _factorial(num));
    },
    randomInt: function(int,offset){
        if(offset == undefined || null || NaN){
            offset = 0;
        }
        return Math.floor(Math.random()*int)+offset;
    },
    display: function(m, fn){
        fn(m);
    }
};

修改数组本身时,您必须反向循环,以免破坏像这样的循环...

for (var i=arr.length-1; i>=0; i--){}

I fixed it. 我修好了它。 I wanted after each character for there to be a number. 我想在每个字符后加一个数字。 Using the pre-looped array length and doubling it while iterating twice, means that the splice adds the number after the new number element and then the character. 使用预循环的数组长度并在两次迭代时将其加倍,这意味着接合将数字添加到新的数字元素之后,然后添加字符。

Edit: My typo was the problem. 编辑:我的错字是问题。 I didnt even have to use len, just iterate by 2. 我什至不必使用len,只需迭代2。

for(var i = 0;i < arr.length;i+=2){
  arr.splice((i+1),0,lib.randomInt(9));
}

(function() {
    return function(string) {
        var arr = string.split("")
        arr.sort();
        var len = arr.length
        for(var i = 0;i < len*2;i+=2){
            arr.splice((i+1),0,lib.randomInt(9));
        }
        var pseudocryptarr = arr.join("");
        console.log(pseudocryptarr);
    }
})()("example");

Edit: user4723924 method is better: 编辑:user4723924方法更好:

(function() {
    return function(string) {
        var arr = string.split("")
        arr.sort();
        for(var i = arr.length;i >= 0;i--){
            arr.splice((i+1),0,lib.randomInt(9));
        }
        var pseudocryptarr = arr.join("");
        console.log(pseudocryptarr);
    }
})()("example");

I guess that you wanted to insert a random value after every array element, so that the string "example" would become something like "e5x9a2m4p7l1e3" 您想在每个数组元素后插入一个随机值,以便字符串“ example”将变成类似于“ e5x9a2m4p7l1e3”

There are two issues: 有两个问题:

  • Your for loop has no end condition that will become false. 您的for循环没有结束条件,该条件将变为false。 You need to state i < arr.length instead of just arr.length which is always truthy for non-empty arrays. 您需要声明i < arr.length而不是arr.length ,这对于非空数组总是正确的。

  • You add array elements in every iteration, but then also visit them in the next iteration, and from there on you will only be visiting the new inserted values and never get to the next original element that keeps being 1 index away from i . 您可以在每次迭代中添加数组元素,但在下一次迭代中也要访问它们,从那以后,您将只访问新插入的值,而永远不会到达与i保持1索引的下一个原始元素。 You need to increment i once more. 您需要再次增加 For that you can use ++i instead if i+1 as the splice argument. 为此,如果i+1作为splice参数,则可以使用++i

So your loop should be: 因此,您的循环应为:

    for(var i = 0; i < arr.length; i++) {
        arr.splice(++i,0,lib.randomInt(9));
    }

 const lib = { randomInt: n => Math.floor(Math.random()*n) }; (function() { return function(string) { var arr = string.split("") arr.sort(); for(var i = 0; i < arr.length; i++) { arr.splice(++i,0,lib.randomInt(9)); } var pseudocryptarr = arr.join(""); console.log(pseudocryptarr); } })()("example"); 

Or to save an addition: 或保存一个附加项:

    for(var i = 1; i <= arr.length; i+=2) {
        arr.splice(i,0,lib.randomInt(9));
    }

 const lib = { randomInt: n => Math.floor(Math.random()*n) }; (function() { return function(string) { var arr = string.split("") arr.sort(); for(var i = 1; i <= arr.length; i+=2) { arr.splice(i,0,lib.randomInt(9)); } var pseudocryptarr = arr.join(""); console.log(pseudocryptarr); } })()("example"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM