这是交易:我正在尝试通过名称使用不同的变量从JSON文件中的变量获取数据。基本上,我想通过将“M83”替换为“Bands”来访问MusicJSON.Bands.M83.length,否则,我必须将JSON文件中的每个变量添加到我的代码中,这对我来说是失败的。这是我的代码:
for(i = 0; i < MusicJSON.Bands.Names.length; i++) {
var Band = MusicJSON.Bands.Names[i]
NextMessage += "\n " + Band + MusicJSON.Bands.length + " songs"; // Right here
}
NextMessage += "**";
message.channel.send(`Here's a list of bands available on this server: \n ${NextMessage}`)
答案 0 :(得分:1)
我很难得到你想做的事。如果您的OracleDependency
是这样的:
MusicJSON.Bands
你想得到每个乐队的长度,试试这个:
{
Names: ["1", "2", "3", "M83"],
1: ["song1", "song2", "song3"],
2: ["song1", "song2", "song3"],
3: ["song1", "song2", "song3"],
M83: ["Midnight City", "Wait", "Outro"]
}
另外,请记住,如果名称是对象中的键,则不需要存储名称,因为您可以像这样遍历键:
var next = "";
for (let name of MusicJSON.Bands.Names) { //automatically loop through the array
let number = MusicJSON.Bands[name].length; //access the object like MusicJSON.Bands["M83"]
next += `\n${name}: ${number} songs` //add the text
}
next += "**";
message.channel.send(`Here's a list of bands available on this server:\n${next}`);
我希望这是你想做的事情