【发布时间】:2020-02-21 21:46:25
【问题描述】:
我目前正在编写一个不和谐的机器人,它正在为其用户保存经济数据。下面的代码预计会循环保存在文件夹中的每个文件(以用户 ID 作为其名称的 JSON),其中包含他们拥有的“建筑物”的 ID 数组。
这是我正在使用的表格的缩小 JSON,它将在其中找到用户拥有的建筑物的数据(请注意,每个类别都有多个类别和“建筑物”:
{
"small_buildings" : [
{
"description" : "basic description",
"name" : "basic item",
"income" : 500,
"startprice" : 30000,
"id" : 1
}
]
}
下面的代码循环每个文件,然后循环每个类别,但第三个循环尝试循环每个建筑物(如“for(var e in econvar.category){”行)从 0-15 计数并且找不到与 id 匹配的内容。
“econvar”标记json表的文件位置。
files.forEach(function (file) {
let jsonArr = require("C:/discordbot/econdata/" + (file)) // get the initial file
jsonArr.forEach(function(i){ // for each number in the array of the economy data file
console.log("got to 2 " + i)
for(var category in econvar) { // checks for each category in the economy.json file for a match
console.log("got to 3 " + category)
for (var e in econvar.category){ // checks each building in the category for a match
console.log("got to 4 " + e)
if (e.id == i) {
console.log("got to 5 " + e.income)
total += e.income;
channel.send("totalcount: " + total)
}
}
}
})
});
我已尝试更改变量 e、变量 econvar.category 和不同的循环技术,但我要么得到“得到 4 个未定义”、“得到 4(然后每行 0-15)”,要么出现错误.
谢谢。我希望它不是太具体而无法理解。
【问题讨论】:
-
我想补充一下,我是初学者,如果不是很明显的话。我已经编写 JavaScript 代码几个月了。
标签: javascript arrays json loops