Using the method from the Is the hasOwnProperty method in JavaScript case sensitive? post, I was able to use it as expected: to check if the json object had the key name while ignoring the case. However, when looping through my JSON object, the method name would print out for a key and the method details would print out as a value.
Object.prototype.hasOwnPropertyCI = function(prop) {
return Object.keys(this)
.filter(function (v) {
return v.toLowerCase() === prop.toLowerCase();
}).length > 0;
};//checks item exists in json ignoring case
function findOwnPropertyCI(obj, property) {
var props = [];
for (var i in obj) if (obj.hasOwnProperty(i)) props.push(i);
var prop;
while (prop = props.pop()) if (prop.toLowerCase() === property.toLowerCase()) return prop;
return false;
}//finds actual casing once confirmed exists
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.msgs = require ('./hiscores.json');
bot.on('message',message=>{
let input = message.content;
if(input.startsWith('$'))
{
let command = input.substr(PREFIX.length).split(" ")[0];
let args = input.substr(input.indexOf(' ')+1);
switch(command){
case 'personal':
playername = args
if(bot.msgs.hasOwnPropertyCI(playername)){
var pers = findOwnPropertyCI(bot.msgs, playername);
message.channel.send(pers);
for(var bosses in bot.msgs[pers]){
message.channel.send(bosses);
for(var drops in bot.msgs[pers][bosses]){
message.channel.send(drops+": "+bot.msgs[pers][bosses][drops]);
}
}
break;
}
}
});
hiscores.json
{
"Player 1": {
"Boss 1": {
"Drop 1": 0,
"Drop 2": 0,
"Drop 3": 1,
"Drop 4": 0
},
"Boss 2": {
"Drop 5": 0,
"Drop 6": 0,
"Drop 7": 0,
"Drop 8": 0,
"Drop 9": 0,
"Drop 10": 0
}
},
"Player 2": {
"Boss 1": {
"Drop 1": 0,
"Drop 2": 0,
"Drop 3": 1,
"Drop 4": 0
},
"Boss 2": {
"Drop 5": 0,
"Drop 6": 0,
"Drop 7": 0,
"Drop 8": 0,
"Drop 9": 0,
"Drop 10": 0
}
}
}
Input:
$personal Player 1
Output:
Player 1
Boss 1
Drop 1: 0
Drop 2: 0
Drop 3: 1
Drop 4: 0
hasOwnPropertyCI: function(prop) {
return Object.keys(this)
.filter(function (v) {
return v.toLowerCase() === prop.toLowerCase();
}).length > 0;
}
Boss 2
Drop 5: 0
Drop 6: 0
Drop 7: 0
Drop 8: 0
Drop 9: 0
Drop 10: 0
hasOwnPropertyCI: function(prop) {
return Object.keys(this)
.filter(function (v) {
return v.toLowerCase() === prop.toLowerCase();
}).length > 0;
}
hasOwnPropertyCI
hasOwnPropertyCI: function(prop) {
return Object.keys(this)
.filter(function (v) {
return v.toLowerCase() === prop.toLowerCase();
}).length > 0;
}