使用.filter比较两个数组并返回不匹配的值

Pyr*_*eal 5 javascript

我在比较两个数组的元素并过滤掉匹配值时遇到了一些问题.我只想返回未包含在其中的数组元素wordsToRemove.

var fullWordList = ['1','2','3','4','5'];
var wordsToRemove = ['1','2','3'];

var filteredKeywords = fullWordList.forEach(function(fullWordListValue) {
    wordsToRemove.filter(function(wordsToRemoveValue) {
        return fullWordListValue !== wordsToRemoveValue
    })
});

console.log(filteredKeywords);
Run Code Online (Sandbox Code Playgroud)

Alb*_*res 11

您可以使用filterincludes实现此目的:

var fullWordList = ['1','2','3','4','5'];
var wordsToRemove = ['1','2','3'];

var filteredKeywords = fullWordList.filter((word) => !wordsToRemove.includes(word));

console.log(filteredKeywords);
Run Code Online (Sandbox Code Playgroud)