简体   繁体   English

返回具有给定短语(数组)值的键

[英]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.

相关问题 从包含特定短语的 JSON 解析返回数组值 - Return array value from JSON parse that contains a certain phrase 匹配对象数组中的对象键并返回具有最高音量值的值 - Match Object keys in array of objects and return key, values that have highest volume value Javascript Array 2 键返回一个值 - Javascript Array 2 keys return one value 检查现有数组是否具有给定值 - Checking existing array have given value or not 基于给定值的键的打字稿返回数组 - typescript return array of key based on given value 给定数组,编写一个函数并返回一个布尔值 - Given array, write a function and return a boolean value 合并 JSON 对象中的键以使用 Javascript 返回合并键的数组和值 - Merge keys in JSON object to return an array of merged keys and the value with Javascript 如果给定则按索引返回数组值,否则返回数组 - return array value by index if given else return the array 鉴于键在 Javascript 中的数组中给出,如何获取 Json 中存在的键的值? - How to get value of keys present in a Json given that the keys are given in an array in Javascript? 是否有一个函数可以在JavaScript中给出一个键数组和一个默认值 - Is there a function to make an object given an array of keys and a default value in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM