Javascript 在数组中找不到值?
我正在尝试适度处理一些事情,我不能将数组用于单词列表,因为它不允许我检查消息是否包含数组中的任何值。如何查看一个数组(words)中的值是否与另一个数组(lang.blacklist)中的任何值匹配?以下是我的代码片段:Javascript 在数组中找不到值?,javascript,discord,discord.js,Javascript,Discord,Discord.js,我正在尝试适度处理一些事情,我不能将数组用于单词列表,因为它不允许我检查消息是否包含数组中的任何值。如何查看一个数组(words)中的值是否与另一个数组(lang.blacklist)中的任何值匹配?以下是我的代码片段: let words = message.content.split(' '); if (words.includes(lang.blacklisted.values())) { message.channel.send('word') } 如果
let words = message.content.split(' ');
if (words.includes(lang.blacklisted.values())) {
message.channel.send('word')
}
如果您想检查是否有任何单词是您的黑名单的一部分(我假设您的黑名单是一组),您可以这样做
let words = message.content.split(' ').map( w => w.toUpperCase() );
if (words.some( word => lang.blacklisted.has( word ) ) ) {
message.channel.send('word');
}
如果你想找到触发它的确切单词
let words = message.content.split(' ').map( w => w.toUpperCase() );
let blacklisted = words.find( word => lang.blacklisted.has( word ) );
if (blacklisted) {
message.channel.send( blacklisted );
}
如果可以是一系列的话
let words = message.content.split(' ').map( w => w.toUpperCase() );
let blacklisted = words.filter( word => lang.blacklisted.has( word ) );
if (blacklisted.length) {
// this will send all words found in lang.blacklisted
message.channel.send( blacklisted );
}
以上几行假设:
是一个lang.blacklist
值是大写的lang.blacklist