使用.filter比较两个数组并返回不匹配的值 [英] Using .filter to compare two arrays and return values that aren't matched
本文介绍了使用.filter比较两个数组并返回不匹配的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在比较两个数组的元素并过滤掉匹配值时遇到了一些问题。我只想返回 wordsToRemove
中未包含的数组元素。
I'm having some issues comparing the elements of two arrays and filtering out matching values. I only want to return array elements that are NOT included within 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);
推荐答案
You can use filter
and includes
to achieve this:
var fullWordList = ['1','2','3','4','5'];
var wordsToRemove = ['1','2','3'];
var filteredKeywords = fullWordList.filter((word) => !wordsToRemove.includes(word));
console.log(filteredKeywords);
这篇关于使用.filter比较两个数组并返回不匹配的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文