正确读取Array



我有一个json文件"GuideDB"它包含了关于某事物的数据

GuildDB.json
{"GuideID":{"prefix":"","DJ":null,"Roles":""}}

基本上我想访问数据"角色"在某种程度上,我可以管理该行中的每个元素,例如:

const myArray = GuildDB.Roles; //GuildDB.Roles is the only way to access that line.
myArray.forEach(element => { 
console.log("<@&" + element + ">") 
});

这段代码给出了错误"forEach",因为数组没有正确格式化。

如何在GuildDB.Roles中添加项目?

client.database.guild.set(message.guild.id, {
prefix: GuildDB.prefix,
DJ: GuildDB.DJ,
Roles: GuildDB.Roles + ", " + createdRole.id, //Bug: First run always writes the comma first.
});

结果如下:

{"GuideID":{"prefix":"","DJ":null,"Roles":", 123, 1234, 12345, 123456"}}
这不是我想要的。我不在乎这看起来如何,我需要使它易于访问读取/管理角色的数据。

希望大家都能帮忙。

您可以这样做,首先将Roles转换为数组,然后在其上使用forEach循环。

const myArray = GuildDB.Roles.split(",");

最新更新