【问题标题】:Read local JSON with Javascript [Discord bot]使用 Javascript 读取本地 JSON [Discord 机器人]
【发布时间】:2019-05-08 14:07:51
【问题描述】:

我想提取我的 .json 文件的数据,并按给定的名称在不和谐频道过滤中显示它。 在这种情况下,msg 变量包含我要搜索的名称。

var msg;
if (message.content.startsWith(prefix + prefix)) {
    msg = message.content;
    message.channel.send(msg.substring(2)); //this is just for testing
}

现在有了这个名字,我不知道如何在我的 .json 文件中搜索并提取数据。 这是我的 .json 文件的一部分

{
  "ID": "1",
  "Nombre": "luffy caca",
  "Tipo": "str",
  "Clase principal": "fighter"
},
{
  "ID": "2",
  "Nombre": "luffy manco",
  "Tipo": "int",
  "Clase principal": "slasher"
},
{
  "ID": "3",
  "Nombre": "luffy g2",
  "Tipo": "str",
  "Clase principal": "driven",
  "Clase secundaria": "slasher"
}

所以我想在我的msg 变量上获得名称“luffy g2”时提取“ID:3”的数据。

【问题讨论】:

    标签: javascript json bots discord


    【解决方案1】:

    试试这个:

    var msg =[
        {
          "ID": "1",
          "Nombre": "luffy caca",
          "Tipo": "str",
          "Clase principal": "fighter"
        },
        {
          "ID": "2",
          "Nombre": "luffy manco",
          "Tipo": "int",
          "Clase principal": "slasher"
        },
        {
          "ID": "3",
          "Nombre": "luffy g2",
          "Tipo": "str",
          "Clase principal": "driven",
          "Clase secundaria": "slasher"
        }
      ];
      
      var idToFind = msg.find(x => x.Nombre == "luffy g2").ID;
      
      console.log(idToFind);

    find 方法将:

    数组中第一个满足 提供测试功能。否则返回 undefined。

    【讨论】:

    • 是的,这就是想法,但我试图从 .json 文件中读取而不是从变量中读取。
    • message.content 是什么样的?
    猜你喜欢
    • 2020-06-03
    • 2021-03-12
    • 2021-01-21
    • 2022-12-03
    • 2019-01-15
    • 1970-01-01
    • 2021-11-08
    • 2021-07-17
    • 2021-05-11
    相关资源
    最近更新 更多