gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:43:56 25 4
gpt4 key购买 nike

我在比较两个数组的元素和过滤掉匹配值时遇到了一些问题。我只想返回未包含在 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);

最佳答案

您可以使用 filterincludes实现这一目标:

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

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

console.log(filteredKeywords);

关于javascript - 使用 .filter 比较两个数组并返回不匹配的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com