I'm looking for a way to access elements in an array using unique key names instead of numerical indexes. Specifically, I'm developing a Discord bot where each server has its own settings. When a message is sent on a server, I need to retrieve that server's specific settings, such as the message prefix. Currently, I have to loop through all the servers the bot is in to find the right settings, but this process could slow down the bot if there are hundreds of active servers. I'm seeking a more direct method to access these settings without having to loop through the array.
Here is an example of the settings in conf.json:
{
"Settings": [
"358262452343013386" {
"prefix": "$",
"Admins": [
"155444308395294720"
],
"NotificationChannel": "358772856282284033",
"robotpieces": []
}
]
}
What I want to be able to do in my bot.js:
console.log(conf.Settings[message.guild.id].prefix); // outputs the prefix
// message.guild.id is the id of the server, which in this case, would translate to this:
console.log(conf.Settings["358262452343013386"].prefix) // outputs '$'
Does anyone have any suggestions on how I can achieve a similar goal without having to iterate through the array?
EDIT: I understand that the JSON structure provided is invalid, but I'm looking for a solution that would provide the same functionality.