Home > Mobile >  Discord js How I can search for userID in json file using for...in
Discord js How I can search for userID in json file using for...in

Time:09-25

The problem I have is when I use this loop, I only get the guild id, but I need to get the user id. My Code:

for(let userID in money){
    res.push({"guildid": message.guild.id, "id": userID, "money": money[message.guild.id][userID].money })
    res.sort((a, b) => Object.values(b)[1] - Object.values(a)[1])
    console.log(userID);
}

My JSON file:

{
"2543634674355 <======GuildID":
    {"309781817504956426 <======userID":
     {       "name":"user1#7777",
             "money":15000},
     "868630066064654397 <======userID":
{            "name":"user2#7777",
             "money":0}},
"402469770760421406 <======GuildID":
{    "309781817504956426 <======userID":
{            "name":"user1#7777",
             "money":0
}
}
}

CodePudding user response:

for (let userID in money[message.guild.id]) {
  // Your code
}

Very basic javascript, very basic logic. Just go through the logic: you want to loop through all of the user ID data in the current guild, contained in the JSON file (assuming the money variable contains the JSON data). So get the current guild data from the JSON data (money[message.guild.id]), and then loop through the user IDs in it with the for/in loop.

Nothing to it. You may want to go through some Javascript tutorials and get more acquainted with Javascript while/before diving into discord bot programming, so you can better understand how to code this type of logic.

  • Related