In the midst of developing a discord bot that stores economic data for its users. The script below is designed to iterate through each file saved in a directory (JSONs named after the user's ID), containing an array of "buildings" or items they possess.
Here's a condensed JSON snippet of the table I'm utilizing to retrieve information on the buildings owned by the user (note: there are multiple categories and various buildings within each category):
{
"small_buildings" : [
{
"description" : "basic description",
"name" : "basic item",
"income" : 500,
"startprice" : 30000,
"id" : 1
}
]
}
The code provided iterates through each file, followed by each category. However, the third loop attempting to cycle through each building (specifically at "for (var e in econvar.category){") runs from 0-15 without finding a suitable id match.
"econvar" denotes the file path of the json table.
files.forEach(function (file) {
let jsonArr = require("C:/discordbot/econdata/" + (file)) // fetch the initial file
jsonArr.forEach(function(i){
console.log("reached step 2 " + i)
for(var category in econvar) {
console.log("reached step 3 " + category)
for (var e in econvar.category){
console.log("reached step 4 " + e)
if (e.id == i) {
console.log("reached step 5 " + e.income)
total += e.income;
channel.send("totalcount: " + total)
}
}
}
})
});
I've experimented with altering the variables e and econvar.category along with employing different looping methods, yet I still encounter issues like "reached step 4 undefined," "reached step 4 (followed by numbers 0-15)," or encountering errors.
Your assistance in resolving this matter would be greatly appreciated. Thank you for your patience in understanding the intricacies involved.