Organize Json file

0

I wanted a way to organize this Json file:

{
    "ajuda": {
        "name":"Ajuda",
        "category": "Sistema",
        "desc":"Mostra todos os comandos disponiveis.",
        "usage":"ajuda [grupo ou comando]"
    },  
    "ping": {
        "name":"Ping",
        "category": "Sistema",
        "desc":"Usado para saber a latencia do bot.",
        "usage":"ping"
    },  
    "status": {
        "name":"Status",
        "category": "Sistema",
        "desc":"Mostra algumas estatística úteis do bot.​",
        "usage":"status"
    },
    "convidar": {
        "name":"Convidar",
        "category": "Miscelânea",
        "desc":"Usado para mostrar o meu link de convite.",
        "usage":"convidar"
    },
    "kick": {
        "name":"Kick",
        "category": "Administração",
        "desc":"Usado para expulsar a pessoa mencionada.",
        "usage":"kick <@user>"
    }


}

In alphabetical order by category. And put it in that code here:

let currentCategory = "";
        let output = '= Lista de Comandos =\n\n[Use ${config.prefix}ajuda <comandos> para detalhes]\n';


        for (var cmd in commands) {

                const cat = commands[cmd].category;
                  if (currentCategory !== cat) {
                output += '\u200b\n== ${cat} ==\n';
                    currentCategory = cat;
                  }

                  output += '${config.prefix}${commands[cmd].name} :: ${commands[cmd].desc}\n'


        }


        message.channel.send(output, {code: "asciidoc", split: { char: "\u200b" }});

But I was not able to ... I tried 300 ways that I found on the internet but nothing.

    
asked by anonymous 22.01.2018 / 01:31

1 answer

1

Since it is an object and not an array, you have to sort through the keys. To get the keys you can use the % object% method passing the commands as a parameter. Then sort the keys using the method keys in which specifies sorting by category.

Example:

const json = '{
    "ajuda": {
        "name":"Ajuda",
        "category": "Sistema",
        "desc":"Mostra todos os comandos disponiveis.",
        "usage":"ajuda [grupo ou comando]"
    },  
    "ping": {
        "name":"Ping",
        "category": "Sistema",
        "desc":"Usado para saber a latencia do bot.",
        "usage":"ping"
    },  
    "status": {
        "name":"Status",
        "category": "Sistema",
        "desc":"Mostra algumas estatística úteis do bot.​",
        "usage":"status"
    },
    "convidar": {
        "name":"Convidar",
        "category": "Miscelânea",
        "desc":"Usado para mostrar o meu link de convite.",
        "usage":"convidar"
    },
    "kick": {
        "name":"Kick",
        "category": "Administração",
        "desc":"Usado para expulsar a pessoa mencionada.",
        "usage":"kick <@user>"
    }
}';

let comandos = JSON.parse(json);

//ordenação por category
let chavesOrdenadas = Object.keys(comandos).sort(
    (a,b) => comandos[a].category.localeCompare(comandos[b].category)
);

for (let chave of chavesOrdenadas){
    console.log(comandos[chave]);
}

Sorting was done by referring to the command category in sort and then using comandos[a].category of localeCompare to compare categories.

From this moment you just have to use the ordered keys whenever you want to do some operation on the all commands.

Applying to your String would look like this:

let orderedCommands = Object.keys(commands).sort(
    (a,b) => commands[a].category.localeCompare(commands[b].category)
);

for (let cmd of orderedCommands /*<--aqui as chaves ordenadas*/) {
    const cat = commands[cmd].category;
    if (currentCategory !== cat) {
        output += '\u200b\n== ${cat} ==\n';
        currentCategory = cat;
    }

    output += '${config.prefix}${commands[cmd].name} :: ${commands[cmd].desc}\n';
}
    
22.01.2018 / 02:18