例如。我想检查一下,如果我的字符串消息包含一些来自 MySQL 对象(黑名单)的关键字
var message = "My keyword is blacklist";
MySQL 对象
[
{ keyword: 'blacklist'},
{ keyword: 'some'},
{ keyword: 'other'},
{ keyword: 'words'},
]
答案 0 :(得分:1)
最佳解决方案如下
const message = 'My keyword is blacklist';
const blockedWords = [
{ keyword: 'blacklist'},
{ keyword: 'some'},
{ keyword: 'other'},
{ keyword: 'words'},
];
function isBlocked(message, blockedWords){
for (const {keyword} of blockedWords){
if (message.includes(keyword)){
return true;
}
}
return false;
}
const result = isBlocked(message, blockedWords);
console.log(result);
console.log(isBlocked('Love JS', blockedWords));
答案 1 :(得分:0)
我将信息“消息字符串是否至少包含一个 MySQL 对象”存储在“包含”布尔值中:
var message = "My keyword is blacklist";
var contains = false;
var mysqlObject = [
{ keyword: "blacklist" },
{ keyword: "some" },
{ keyword: "other" },
{ keyword: "words" },
];
for (var i = 0; i < mysqlObject.length; i++) {
if (message.includes(mysqlObject[i].keyword)) {
contains = true;
}
}
console.log(contains);