Organize Json file

Asked

Viewed 529 times

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 fit it into this 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’m not getting... I’ve tried 300 ways I found on the internet but nothing.

  • Sort alphabetically but by category. So I want him to put all of the administration category first, then all of the miscellaneous category and then all of the system category.

1 answer

1


Since it is an object and not an array you have to sort by the keys. To get the keys you can use the method keys object passing the commands as parameter. Then sort the keys using the method sort which specifies the sort 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]);
}

The sort was done by referring to the category of the command in comandos[a].category and then using localeCompare of String to make the comparison between categories.

From now on you just have to use the sorted keys whenever you want to do some operation on all commands.

Applying to your for 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`;
}
  • Oops! I really liked your answer and helped me solve the problem! But when I went to apply in mine for I had to replace in my repeating structure the var for let and the in for of. Like in your first example.

  • @Henry Yes, I’m glad you warn me, in fact it’s really necessary Keys with for .. of or for normal

Browser other questions tagged

You are not signed in. Login or sign up in order to post.