迭代

时间:2017-02-17 03:15:55

标签: javascript loops

我有以下代码:

for (let word in words) {
  for (let i = 0; i < words.length; i++) {
    // something
    words.splice(i, 1);
    i--;
  }
}

而且我更聪明我怎么能继续外部迭代,即使我用拼接改变了尺寸。我读到我可以通过反向迭代实现这一点,但这个解决方案只适用于一个循环,而不是两个。

感谢。

1 个答案:

答案 0 :(得分:0)

如果要移除元素,最好向后迭代数组。

根据我们的讨论,您需要将元素与其他元素进行比较,并且基于某些业务逻辑(在下面的代码中表示为随机的10%)将拒绝提供的数组中的单词。

<强> JS

let words = ["one", "two", "three", "four", "five", "six"]; // 100.000 strings 

for (let x = words.length - 1; x >= 0; x--) {
    // if we're trying to access an element that no longer exists
    if (typeof words[x] === 'undefined') continue;
    for (let y = words.length - 1; y >= 0; y--) {
        // if we're trying to access an element that no longer exists
        if (typeof words[y] === 'undefined') continue;
        // if word should be rejected
        console.log('Comparing: ' + words[x] + ' ' + words[y]);
        if (shouldRejectWordB(words[x], words[y])) {
            // remove the word
            console.log('Rejecting: ' + words[y]);
            words.splice(y, 1);
        }
    }
}
console.log(JSON.stringify(words));

function shouldRejectWordB(wordA, wordB) {
    // reject word randomly
    if (Math.random() < 0.1) {
        return true;
    } else {
        return false;
    }
}

<强> JS FIDDLE

效率更新

进一步考虑这一点后,递归函数似乎比上面的函数更有效。以上只是continue的任何遇到的元素都在undefined索引处。因此,我们仍在处理n^2元素。或者,如果我们将当前的单词列表作为参数传递给递归函数,并与wordA进行比较,我们可以减少否定尝试访问已删除的单词,从而在删除项目时加快后续迭代。 / p>

<强> JS

let words = ["one", "two", "three", "four", "five", "six"]; // 100.000 strings 

function shouldRejectWordB(wordA, wordB) {
    // reject word randomly
    if (Math.random() < 0.1) {
        return true;
    } else {
        return false;
    }
}

function compareWordAToEachWord(wordA, words){
    // loop through each provided word
    for (let y = words.length - 1; y >= 0; y--) {
        // if word should be rejected
        console.log('Comparing: ' + wordA + ' ' + words[y]);
        var wordToCompare = words[y];
        if (shouldRejectWordB(wordA, wordToCompare)) {
            // remove the word
            console.log('Rejecting: ' + wordToCompare);
            words.splice(y, 1);
            // if we just rejected the word that is currently set as wordA stop comparing this loop
            if(wordToCompare === wordA){
                break;
            }
        }
    }
    if(words.length){
        // the index of the current wordA
        var index = words.indexOf(wordA);
        // suggested index of the next word
        var newIndex = words.length - 1;
        console.log('index: '+index+' suggestion: '+newIndex);
        // if the suggestion is the same or greater than the current word, get the item before the current word
        if(index <= newIndex){
            newIndex = index-1;
        }
        // if our suggestion is not for an element before the first (ie. invalid), begin another comparison
        if(newIndex >= 0){
            compareWordAToEachWord(words[newIndex], words);
        }else{
            // we have completed out looping through all words for comparison
            console.log(JSON.stringify(words));
        }
    }
}

console.log(JSON.stringify(words));
// kick start the comparison with the last word in the list
compareWordAToEachWord(words[words.length - 1], words);

<强> UPDATED FIDDLE