[英]Return keys that have value with the given phrase (array)
I have problem with JavaScript array.我对 JavaScript 阵列有疑问。 I am trying to create a function that, according to a particular arg, returns keys with a value that has that arg.
我正在尝试创建一个 function,它根据特定的 arg 返回具有该 arg 值的键。
The code I tried:我试过的代码:
for(let i in client.commands.size) {
let filteredCommands = client.commands.filter(cmd => cmd[i].help.cmdCategory = arg).map(c => c.name)
console.log(filteredCommands)
embed.addField(`${filteredCommands.help.name}`, `**Description:** ${filteredCommands.help.desc}\n**Usage:** \`${client.prefix}${filteredCommands.help.usage}\`\n**Exxample Usage:** ${filteredCommands.help.exampleUsage}`, false)
}
client.commands
it's a array who key is name of command, and value in command key (example. ping
) named cmdCategory
is in help
subkey and need value same in argument and next return keys which meet this condition. client.commands
它是一个数组,其键是命令的名称,并且名为cmdCategory
的命令键(例如ping
)中的值在help
子键中,并且需要在参数和下一个满足此条件的返回键中的值相同。 (for example: if key value cmdCategory
have value fun
, then return keys who meet this criteria. Any ideas for here? Thanks anyway. (例如:如果键值
cmdCategory
具有值fun
,则返回符合此条件的键。这里有什么想法吗?还是谢谢。
If your object client looks like this example then you can try it如果你的 object 客户端看起来像这个例子那么你可以试试
let arg = 'sh' let client = { commands: [ [ { help: { cmdCategory: 'bash', name: 'some bash name', desc: 'description for bash' } }, { help: { cmdCategory: 'sh', name: 'some sh name', desc: 'description for sh' } } ] ] } // reduce from [[{}, {}]] to [{},{}] let commands = client.commands.reduce((prev, next) => { return prev.concat(next) }) let filteredCommands = commands.filter(command => command.help.cmdCategory === arg) console.log(filteredCommands) filteredCommands.forEach(cmd => { /* embed.addField(`${cmd.help.name}`, `**Description:** ${cmd.help.desc}\n**Usage:** \`${client.prefix}${cmd.help.usage}\`\n**Exxample Usage:** ${cmd.help.exampleUsage}`, false) */ }) // OR you can do this: filteredCommands = [] client.commands.forEach(cmd => { cmd.forEach(item => { if (item.help.cmdCategory === arg) { filteredCommands.push(item) } }) }) console.log(filteredCommands)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.