Reputation: 219
I'm trying to limit database hits on my RPG Bot in DISCORD and so decided to store an initall lookup of my database as an internal array so that when doing data look up i can just loop through my array and look for where
UserList[i][ID] is equal to the id of the responding user. This is how my database lookup works i'm just offloading it to an array so Id.Of.comment.owner = SomeNumber fetch from mysql database user whos id is SomeNumber
but now i'll be doing more of a For (i =0; i < userlist.length; i++) if userlist[i][ID] = Somenumber --Do something
And i just want to make surei understand how i set up a multi dimensioal array in node.js
Obviously i'll have to prock the database one and loop through that but what is the syntax to init and fill is it just
var Userlist[][];
then
for (h=0; h< sizeofdatabase; h++)
UserList[h][id]
...
UserList[h][PLACEHOLDER_xp_percent]
UserList[h][PLACEHOLDER_rank]
UserList[h][PLACEHOLDER_rank_total]
Upvotes: 0
Views: 474
Reputation: 219
so after talking on discord it turned out that Maps were the way to go, as i haven't found any good map references let me share what i did.
first off the initilizer
exports.UserSkillList = new Map();
Now i put mine in my setting.js where I keep all my globals this the 'exports' but if you don't need it globally it's just
let UserSkillList = new Map();
now lets populate it, i'm populating mine from my user database and i'm just going to assume you know how to make a sql loop, for my purposes rows_db is the array for each pass through my database
for (var i = 0; i < rows_db.length; i++) {
settings.UserList.set(rows_db[i].id, {
Gender: rows_db[i].Gender,
Height: rows_db[i].Height,
Weight: rows_db[i].Weight,
Fitness: rows_db[i].Fitness,
MAX_HP: rows_db[i].MAX_HP,
Charisma: rows_db[i].Charisma,
Intelligence: rows_db[i].Intelligence,
Agily: rows_db[i].Agily
//you get the idea
});
};//for
Now let's say i want to pull the Charisma Value for the user who just posted and i have their id stored as the virable uid
settings.UserList.get(uid).Strength
Lastly lets say i want a count of total users
settings.UserList.size;
Upvotes: 1