javascript - 如何根据另一个数组中的单词过滤数组?
问题描述
我想过滤包含在数组中的一些单词,包含在字符串中。例如:
输入
1.“消失”定义:
[ "Cease to be visible.",
"Cease to exist or be in use.",
"Be lost or go missing, become IMPOSSIBLE to find.",
"Abduct or arrest and kill or detain (a person) for political reasons,
without making their fate known." ]
2.违禁词:
[ 'without', 'impossible' ]
结果
[ "Cease to be visible.", "Cease to exist or be in use." ]
我想,我非常接近答案:
function filterDefinition (defs, badWords) {
const definitionFilter = defs.filter(function(def) {
if (def.includes(badWords || badWords.toUpperCase()) {
return !defs
}
});
return definitionFilter;
}
解决方案
像这样?
function filterDefinition (defs, badWords) {
return defs.filter(function(def) {
return !badWords.some(badWord => def.includes(badWord) || def.includes(badWord.toUpperCase()));
});
}
String.includes(string) 不适用于数组。
推荐阅读
- javascript - React TypeError:this.state.List.map 不是函数
- c - C中的排序和动态分配单链表
- python - /users/login/ __init__() 的 TypeError 采用 1 个位置参数,但给出了 2 个
- node.js - 如何显示 child_process 标准输出的“实时”显示
- docker - 为 micronaut 应用程序构建原生镜像
- javascript - 如何在我的反应本机应用程序中通过api制作paytm的支付链接而不添加服务器
- javascript - MongoDB,NodeJS。从承诺中获取元素
- swift - 在功能期间更新标签
- python - 从一组区间Python中获取不重叠的不同区间
- pattern-matching - 使用元组进行模式匹配时的语法 (Elixir)