I am pretty new to JS and I am trying to code a discord bot. Now i have a question:
If i have a list like this:
{
names: [ 'kick' ],
category: 'Moderation',
description: 'Kicks a user',
syntax: '<User> <Reason>',
hidden: false,
testOnly: true
}
{
names: [ 'send' ],
category: 'Config',
description: 'Sends a message.',
syntax: '<channel> <text>',
hidden: false,
testOnly: true
}
{
names: [ 'timeout' ],
category: 'Moderation',
description: 'Times out a user',
syntax: '<User> <Time>',
hidden: false,
testOnly: true
}``` (this is all a variable)
how can i get for example every command that has the Moderation catagory?
So that i have them in an array.
something like
list.filter(x=>x.category==="Moderation")
should do the trick
please read up on Array.filter to learn more
You can use filter method to filter selectedItems.
var myList = {
names: [ 'kick' ],
category: 'Moderation',
description: 'Kicks a user',
syntax: '<User> <Reason>',
hidden: false,
testOnly: true
}
{
names: [ 'send' ],
category: 'Config',
description: 'Sends a message.',
syntax: '<channel> <text>',
hidden: false,
testOnly: true
}
{
names: [ 'timeout' ],
category: 'Moderation',
description: 'Times out a user',
syntax: '<User> <Time>',
hidden: false,
testOnly: true
}
var selectedCommands = myList.filter(value => value.category == 'Moderation')
Judging from the title you only want a string array of the command names.
let disCommands = [ { names: [ 'kick' ], category: 'Moderation', description: 'Kicks a user', syntax: '<User> <Reason>', hidden: false, testOnly: true }, { names: [ 'send' ], category: 'Config', description: 'Sends a message.', syntax: '<channel> <text>', hidden: false, testOnly: true }, { names: [ 'timeout' ], category: 'Moderation', description: 'Times out a user', syntax: '<User> <Time>', hidden: false, testOnly: true } ]
let modCommands = disCommands.filter(c => c.category === 'Moderation').flatMap(c => c.names);
console.log(modCommands);