简体   繁体   English

如何想到“ forEach”而不是常规的for循环?

[英]How to think of “forEach” instead of regular for loops?

I'm new to JS and am having trouble getting my head around the "forEach" method. 我是JS的新手,无法解决“ forEach”方法的问题。


To help illustrate, let's say that I have 2 arrays. 为了帮助说明,假设我有2个数组。

The first: an array containing 1000 random words from the dictionary. 第一个:包含字典中1000个随机单词的数组。 The second: an array containing 10 "stopwords" (words that I'd like to filter out of the first array). 第二个:包含10个“停用词”(我要从第一个数组中过滤掉的词)的数组。

If I sit down and write a function that takes these two arrays parameters, my intuition would tell me to code it like this: 如果我坐下来编写一个带有这两个数组参数的函数,我的直觉会告诉我编写如下代码:

    function cleanSet(theSet, stoppers){
        for(var i=0;i<theSet.length;i++){
            for(var j=0; j<stoppers.length;j++){
                if(theSet[i] === stoppers[j]){
                    theSet.splice(i,1);
                }
            }
        }
        return theSet;
    }

However, when I try to use forEach while writing this program, it doesn't work: 但是,当我在编写此程序时尝试使用forEach时,它不起作用:

function cleanerSet(theSet, stoppers){
    theSet.forEach(function(setWord,i){
        stoppers.forEach(function(stopper, j){
            if(theSet[i] === stopper[j]){
                theSet.splice(i,1);
            }
        });
    });
    return theSet;
}

Why isn't "cleanerSet" working the way "cleanSet" is? 为什么“ cleanerSet”不按“ cleanSet”的方式工作?

I think this is what you want to do 我想这就是你想做的

function cleanerSet(theSet, stoppers){
    return theSet.filter(function(setWord,i){
        return stoppers.indexOf(setWord) === -1;
    });
}

Runtime: cleanerSet([1,2,3,4,5], [2,4]) // [1, 3, 5] 运行时: cleanerSet([1,2,3,4,5], [2,4]) // [1, 3, 5]

The problem is that splice mutates your array, so you have to keep in mind that if you delete an item from the array that you are currently iterating, your index is not reset. 问题在于,拼接会改变您的数组,因此,请记住,如果从当前正在迭代的数组中删除一项,则不会重置索引。 As far as how to solve it, @Gyandeep provides a good solution. 至于如何解决,@ Gyandeep提供了一个很好的解决方案。 Also, in your first implementation, I think you might've meant: 另外,在您的第一个实现中,我认为您可能是说:

theSet.splice(i,1);

Within the callback you pass to the foreach function, the currently iterated value is stored in the first parameter of the function and the current index is stored in the second parameter. 在传递给foreach函数的回调中,当前迭代的值存储在函数的第一个参数中,而当前索引存储在第二个参数中。 So when you check for equality you don't need to reference the index; 因此,当您检查是否相等时,您无需引用索引。 simply use the parameter (ie setWord or stopper). 只需使用参数(即setWord或stopper)。

Additionally, when you call splice, the second parameter should be the number of items you'd like removed, so you need to pass 1, not 'i'. 此外,在调用splice时,第二个参数应该是您要删除的项目数,因此您需要传递1,而不是'i'。

This modified function should work: 此修改后的功能应该可以工作:

function cleanerSet(theSet, stoppers){
    theSet.forEach(function(setWord, i){
        stoppers.forEach(function(stopper){
            if(setWord === stopper){
                theSet.splice(i,1);
            }
        });
    });
    return theSet;
}

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

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