【发布时间】:2019-09-30 07:29:22
【问题描述】:
我有一个字符串数组,我想用另一个单词数组进行过滤。 如果它包含第二个参数的一个单词,则目标是删除整个字符串。
我们以此为例:
comments = [ "Very useful tutorial, thank you so much!",
"React is not a damn framework, it's a LIBRARY"
"Why you put bloody kitten pictures in a tech tutorial is beyond me!",
"Which one is better, React or Angular?",
'There is no "better", it depends on your use case, DAMN YOU'
]
bannedWords = ['bloody', 'damn']
我的代码返回了comments 的完整数组,我不明白如何过滤bannedWords 数组的所有元素。
我很确定我缺乏基本的东西,但我现在被困了一段时间......谢谢!
这是我的代码:
return comments.filter(comment => comment.includes(bannedWords) == false)
};
【问题讨论】:
-
推荐阅读:Obscenity Filters: Bad Idea, or Incredibly Intercoursing Bad Idea?(由StackOverflow创始人本人)
-
你研究过这个吗? StackOverflow 上还有很多其他几乎完全相同的答案。甚至没有人应该回答这个问题,每个人都应该将其标记为重复
-
您的问题在这里
comment.includes(bannedWords)在这里您将完整的数组作为parameter传递给includes,因为您需要一次匹配传递的个人名称
标签: javascript arrays filter