1

I'm trying to categorize my commands and make an array or object like:

my commands = [Music[mymusiccommand1,mymusiccommand2], Fun[myfuncommand1,myfuncommand2]]

I've got all my commands into different files and I'm exporting their categories

module.exports ={category:"Music",name:"Play",description: "Plays music"},

I can access their categories and make an array using them

commands = commands.array();
categorydup =  commands.map(function (obj){return obj.category});
categories = [...new Set(categorydup)]

console.log(categories);

Console:

[ 'Music', 'Fun', 'etc.', 'etc.' ]

But I can't go further and I can't push my commands into them because they are not arrays, but I couldn't figure out how to turn category names into arrays or objects.

Note: I want to get category names from module.exports; not write them by hand

BleedFeed
  • 33
  • 4
  • Might you want an object instead of an array. The object would look like `mycommands ={Music: [mymusiccommand1,mymusiccommand2], Fun:[myfuncommand1,myfuncommand2]}` Each array of commands would be associated with a different property of your `mycommands` object – Richard Jan 02 '21 at 15:22
  • Does this answer your question? [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – Aplet123 Jan 02 '21 at 15:23
  • Build an object like `const objects = [{ Music: { commands: ["command1", "command2"] }, Fun: { commands: ["command1" , "command2"] } }] objects.forEach(val => console.log(val))` – Aalexander Jan 02 '21 at 15:24
  • found the answer on https://www.robinwieruch.de/javascript-groupby thanks for all your comments – BleedFeed Jan 02 '21 at 15:37

0 Answers0