LycalopX
LycalopX

Reputation: 39

How can I loop through a non determined JSON key and send a message with it?

So, I made a command which registers pokémon you are shiny hunting for, how many you have got, and how many eggs it took/is taking for you to get the shiny, and it works fine. My problem is that some people forget the pokémon they did register in that storage, and so I would like to be able to send a message containing all the pokémon they registered in the Storage, like this:

You have registered the following pokémon:  
(All the pokémon you registered)

But, I don't know how to loop through the keys that are inside of the user's ID. Here's how my json file looks:

{
    "User's ID": {
        "Pokémon": {
            "encounters": value,
            "shinies": value
        },
        "Pokémon": {
            "encounters": value,
            "shinies": value
        }
    }
}

I have tried looping like this:

 const path = require('path');
 const fs = require('fs');

 const shiniesDataPath = path.resolve(__dirname, './Storage/shiniesData.json');

 function loadShiniesData() {
     return JSON.parse(fs.readFileSync(shiniesDataPath).toString());
 }

 const shiniesData = loadShiniesData();

 var trainer = shiniesData[message.author.id];

 if (args[0] === 'all') {
     for (var trainer in shiniesData) {
         message.channel.send([trainer])
     }
     return;
 }

But it only loops through the ID's.

Upvotes: 1

Views: 66

Answers (3)

Paul Rooney
Paul Rooney

Reputation: 21609

const message = {
    author: {
        id: "1234"
    },
    channel: {
        send: console.log
    }
}

// stubbed function
function loadShiniesData() {
    return {
        "1234": {
            "Pikachu": {
                "encounters": 2,
                "shinies": 3
            },
            "Fenekin": {
                "encounters": 5,
                "shinies": 1
            }
        }
    }
}

const shiniesData = loadShiniesData();

const trainer = shiniesData[message.author.id];

message.channel.send('You have registered the following pokemon:\n' + Object.keys(trainer).join(", "));

Once you've retrieved the users data then you can use Object.keys to get an array of the pokemon names.

Upvotes: 1

D. Seah
D. Seah

Reputation: 4592

i think the example code that you have provided is not entirely clear. like what do you have var trainer in two places.

var trainer = shiniesData[message.author.id];

 if (args[0] === 'all') {
     for (var trainer in shiniesData) {
         message.channel.send([trainer])
     }
     return;
 }

I am guessing this what you are looking for



if (args[0] === 'all') {
    Object.keys(shiniesData).forEach(userId => {
        const monsterNames = Object.keys(shiniesData[userId]);
        message.channel.send(userId, monsterNames]);
    });
    return;
}

Upvotes: 1

Sal
Sal

Reputation: 1691

A JSON object should not have multiple identical keys, rather what you can do is assign the Pokémon key to an array, like this:

{
    "User's ID": {
        "Pokémon": [
            {
                "encounters": value,
                "shinies": value
            },
            {
                "encounters": value,
                "shinies": value
            }
        ]
    }
}

In order to access the keys, let's say the object above was assigned to variable obj. In order to access the keys in an object, you can do

Object.keys(obj).forEach((key) => {
    // Do something
})

However if you switch to an array as I have done, I'd imagine you would want to do something like this instead:

obj['Pokémon'].forEach((item) => {
    // Do something
})

where obj['Pokémon'] returns the array mapped to the key 'Pokémon', and .foreach iterates through each item in the array, and you can access it within the anonymous function (surrounded by curly braces) via the variable item.

Upvotes: 1

Related Questions