【发布时间】:2021-05-02 00:03:54
【问题描述】:
假设我有一个字符串:
const subject = "This process is flawless"
我有一个数组:
const matchArray = ["process","procedure","job"]
我希望如果主题包含 matchArray 中的任何关键字,
if (subject matches any keyword of matchArray ){
console.log('true')
}
我的第一直觉是使用包含,但我不想将数组与字符串匹配,而是将字符串与数组匹配。
我仍在探索中,如果有人可以指导我,那将非常有帮助。
编辑:我找到了这个灵魂,但有没有比这个更好的解决方案
const subject = "This process is flawless"
const matchArray = ["process","procedure","job"]
const exists = matchArray.some(matchArray => subject.includes(matchArray))
if (exists) {
console.log("Yes");
// notify
}
【问题讨论】:
标签: javascript node.js