NodeJS 如果单词不在列表中,则删除

qjp7pelc  于 2022-12-12  发布在  Node.js
关注(0)|答案(1)|浏览(104)

代码如下:

const { words } = require("../../json/words.json")

const args = message.content.split(' ')
const wordss = words.filter(m=> m.includes(args))
if(args > 1 || !wordss) {
     message.delete()
} else {
message.react("✅")
}

"../../json/words.json"中的单词:

{
  "words": [
 "apple", "ball", "cat", "dog", "earth", "fish", "girl", "house", "ink", "jar", "king", "lock", "moon", "nose", "oat", "pig", "queen", "rope", "shirt", "tea", "umbrella", "vase", "wave", "xylophone", "yolk", "zebra"  
   ]  
 }

如果消息不是现有的单词,我该如何删除该消息?即使该消息不包含列表中的任何单词(例如,如果该消息是“battle”),该消息仍然不会被删除,而是得到的React。

7eumitmz

7eumitmz1#

如果要排除所有不包含words数组中任何一个单词的消息,则以下内容可能对您有所帮助:

const words= [
 "apple", "ball", "cat", "dog", "earth", "fish", "girl", "house", "ink", "jar", "king", "lock", "moon", "nose", "oat", "pig", "queen", "rope", "shirt", "tea", "umbrella", "vase", "wave", "xylophone", "yolk", "zebra"  
   ], msgs=["an apple a day","keeps the doctor away","this is bananas","a cat or a dog"];

const res=msgs.filter(msg=>
 msg.split(' ').some(m=>words.includes(m)))

console.log(res);

相关问题