简体   繁体   中英

Get array from a json file to a javascript array

pre-script: I'm a very noob at javascript and I'm using my c# knoledge to create a Discord bot in javascript, so I'm sorry if this question is too dumb.

I have a code to write down in a json file an array called tarefas using this code:

tarefas.push(subcomando);

bot.tarefasFile ['wait tasks'] = {
    task : tarefas
}
fs.writeFile('./tarefas.json', JSON.stringify(bot.tarefasFile,null,4),
err => {
    if (err) throw err;
});
cmd.channel.send('`' + subcomando + '` added to tasks.');

( subcomando is the message when someone sends w_add task_[subcomando] in my Discord server. Example below)

The output json file tarefas.json is:

{
"wait tasks": {
    "tasks": []
}

For now, my code resets the value of the array tarefas when the bot restarts, so I want to reload its values from the tarefas.json file, precisely from the "tasks": [] array. How do I do that?

extra: Example from when the json file is populated with some "tasks":

screenshot of my discord

{
    "wait tasks": {
        "tasks": [
            "hi",
            "hello",
            "bye",
            "math exam tomorrow"
        ]
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM