#filter help

34 messages · Page 1 of 1 (latest)

copper flame
#
const GeneralCommands = client.commands.map((command) => command.name).join("\n");```
How would I filter out the commad.name that has the word "invite" in it?
fringe fulcrumBOT
copper flame
#

@placid elbow this didnt help ahhh

#

i tried js const InviteCommands = client.commands.filter(command => command.name === "invite").map((command) => command.name).join("\n"); and it just have me errors

placid elbow
#

What errors?

copper flame
#

it just says Received one or more errors

#

but i have a "invite", "invites" command and im trying to sperate it from the batch of other commands

#

so im tryna get a const of just the invites and one without the invites if that makes sense

placid elbow
#

=== means "strictly equals"
Your filter only returns true for "invite", not "invites"

copper flame
#

oh

#

idrk how to fix that then lol

fringe fulcrumBOT
#

mdn String.prototype.includes()
The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.

copper flame
#

@placid elbow i dont want it to return a true or false i just want it to go through the available commands and include them if it finds "invite"

late berry
#

filter takes a predicate

#

so the commands that include in the name "invite" would come out to true and be mapped while the other commands that come out as false wouldnt

copper flame
late berry
#

you can have a return statement with the .map function after the callback which would return the command name

#

wait

#

no

late berry
#

but do what I said earlier in the filter

copper flame
late berry
#

Basically:

const InviteCommands = client.commands.filter(cmd => cmd.name.startsWith("invite")).map(cmd => cmd.name).join("\n")
#

this would be what you would want I think

late berry
#

basically making it so you have a boolean array

copper flame
#

ohh i get it

#

thank you

late berry
#

because your return value is a boolean

late berry
late berry
#

nice