Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从JSON文件访问用户选择的变量?(Node.js)_Json_Node.js_Discord.js - Fatal编程技术网

如何从JSON文件访问用户选择的变量?(Node.js)

如何从JSON文件访问用户选择的变量?(Node.js),json,node.js,discord.js,Json,Node.js,Discord.js,这里有一个交易:我试图从JSON文件中的一个变量获取数据,该变量的名称与另一个变量不同。基本上,我希望通过使用Bands替换M83来访问MusicJSON.Bands.M83.length,否则,我必须将JSON文件中的每个变量添加到我的代码中,这对我来说是没有用的。这是我的密码: for(i = 0; i < MusicJSON.Bands.Names.length; i++) { var Band = MusicJSON.Bands.Names[i]

这里有一个交易:我试图从JSON文件中的一个变量获取数据,该变量的名称与另一个变量不同。基本上,我希望通过使用Bands替换M83来访问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}`)

我正努力得到你想要的。如果你的乐队是这样的:

{
  Names: ["1", "2", "3", "M83"],
  1: ["song1", "song2", "song3"],
  2: ["song1", "song2", "song3"],
  3: ["song1", "song2", "song3"],
  M83: ["Midnight City", "Wait", "Outro"]
}
MusicJSON.Bands = {
  M83: ["Midnight City", "Wait", "Outro"],
  "System Of A Down": ["Toxicity", "Chic 'N' Stu", "Chop Suey"]
}

var names = Object.keys(MusicJSON.Bands).sort(), //create and array with the keys and sort it
  next = "";

for (let band of names) {
  next += `\n${band}: ${MusicJSON.Bands[band].length} songs`;
}
next += "**";
message.channel.send(`Here's a list of bands available on this server:\n${next}`);
如果要获得每个乐队的长度,请尝试以下方法:

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}`);
此外,请记住,如果名称是对象中的关键点,则不需要存储它们,因为您可以像这样循环关键点:

{
  Names: ["1", "2", "3", "M83"],
  1: ["song1", "song2", "song3"],
  2: ["song1", "song2", "song3"],
  3: ["song1", "song2", "song3"],
  M83: ["Midnight City", "Wait", "Outro"]
}
MusicJSON.Bands = {
  M83: ["Midnight City", "Wait", "Outro"],
  "System Of A Down": ["Toxicity", "Chic 'N' Stu", "Chop Suey"]
}

var names = Object.keys(MusicJSON.Bands).sort(), //create and array with the keys and sort it
  next = "";

for (let band of names) {
  next += `\n${band}: ${MusicJSON.Bands[band].length} songs`;
}
next += "**";
message.channel.send(`Here's a list of bands available on this server:\n${next}`);
我希望这是你想做的事