简体   繁体   中英

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:

{
    "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?

If you want to check any prefix string exist in message.content , you should compare result of indexOf with -1 not 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. Returns -1 if the value is not found.

(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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