javascript - 使用 Javascript 读取本地 JSON [Discord 机器人]
问题描述
我想提取我的 .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”的数据。
解决方案
尝试这个:
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
方法将:
数组中满足提供的测试功能的第一个元素的值。否则返回未定义。
推荐阅读
- recursion - 在 OCaml 中折叠列表
- login - 如何在 Hybris 6.7 的自定义 ycommercewebservices 中实现社交登录?
- python - 合并来自包含相同列的两个文件的数据 - Python
- python - 如何在python中找到曲面和线之间的交点?
- python - 在 matplotlib 中为绘图设置临时默认值
- amazon-web-services - 谁为 AWS EC2 上的实例元数据访问提供服务?
- visual-studio-2017 - 解决方案资源管理器中的损坏形式
- javascript - 如何在 Dialogflow 的 JavaScript Web SDK 的 eventRequest 中传递 eventData?
- python - Keras 共享具有不同“可训练”标志的层
- android - Android Studio 3.1.4 抛出 ERROR 启动失败