【问题标题】:Discord js How I can search for userID in json file using for...inDiscord js 如何使用 for...in 在 json 文件中搜索用户 ID
【发布时间】:2021-11-17 21:01:07
【问题描述】:

我遇到的问题是,当我使用这个循环时,我只获取公会 ID,但我需要获取用户 ID。 我的代码:

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);
}

我的 JSON 文件:

{
"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
}
}
}

【问题讨论】:

    标签: javascript node.js json discord.js


    【解决方案1】:
    for (let userID in money[message.guild.id]) {
      // Your code
    }
    

    非常基础的javascript,非常基础的逻辑。只需通过逻辑:您想遍历当前公会中包含在 JSON 文件中的所有用户 ID 数据(假设 money 变量包含 JSON 数据)。所以从 JSON 数据 (money[message.guild.id]) 中获取当前公会数据,然后用 for/in 循环遍历其中的用户 ID。

    没什么。您可能想在深入了解不和谐机器人编程之前/之前阅读一些 Javascript 教程并更加熟悉 Javascript,这样您就可以更好地了解如何编写这种类型的逻辑。

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 1970-01-01
      • 2020-05-02
      • 2019-01-04
      • 1970-01-01
      • 2021-04-28
      • 2021-05-29
      • 1970-01-01
      • 2021-02-07
      相关资源
      最近更新 更多