简体   繁体   English

Javascript:如何将数组中的两个字符串与此语句进行比较?

[英]Javascript: How to compare both strings in an array to this statement?

My code looks like this right now: 我的代码现在看起来像这样:

if (message.content.toLowerCase().indexOf(config.prefix[0]) !== 0) return;
const args = message.content.slice(config.prefix[0].length).trim().split(/+/g);

and my config.json looks like this: 我的config.json看起来像这样:

{
    "prefix": ["mikuru", "miku"]
}

how can I allow message.content.toLowerCase().indexOf(config.prefix[0]) !== 0 or message.content.toLowerCase().indexOf(config.prefix[1]) !== 0 depending on what message.content.toLowerCase() is? 如何允许message.content.toLowerCase().indexOf(config.prefix[0]) !== 0message.content.toLowerCase().indexOf(config.prefix[1]) !== 0具体取决于什么message.content.toLowerCase()是吗?

If you want to check any prefix string exist in message.content , you should compare result of indexOf with -1 not 0. 如果要检查message.content中是否存在任何prefix字符串,则应将indexOf结果与-1而不是0进行比较。

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. indexOf()方法返回指定值首次出现的调用String对象内的索引,从fromIndex开始搜索。 Returns -1 if the value is not found. 如果找不到该值,则返回-1。

(MDN) String.prototype.indexOf (MDN)String.prototype.indexOf

I not really understand your question. 我不太明白你的问题。 What do you really want? 你真正想要的是什么? Can you show the expected result ? 你能显示出预期的结果吗?

I think you can use array includes here. 我认为您可以在此处使用数组包含

In your case, you can use it like this: 在您的情况下,可以这样使用它:

[“mikuru”,“miku”].includes(message.content.toLowerCase())

Or even better: 甚至更好:

prefix.includes(message.content.toLowerCase())

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM