Essentially I need to check whether or not an input is in one object, if not, then check another object.
I have a CSV file with data, which has names of countries in full as the first column of each row.
An example is:
United Kingdom,4,8,1,0
The bot gets the data from c.(countryname or countrycode) e.g. c.uk or c.unitedkingdom would be United Kingdom in the csv file.
I've added lots of comments to hopefully make it easier to read!
else if((msg.content.split('.')[1] != null)){
let arr1 = {
"AD":"Andorra",
"AE":"U A E",
}
let arr11 = Object.fromEntries(Object.entries(arr1).map(e=>e.reverse())); //reversed original object to allow for full country names to be inputted
let arr2 = JSON.parse(JSON.stringify(arr11).replace(/"\s+|\s+"/g,'"')); //strips of blank spaces to match input
var country = msg.content.split('.')[1]; //splits c.uk to ['c.','uk']
if(arr1[country]){ // checking if user inputted a string that is a country code
//EXECUTE on found in first array. e.g. user entered 'ad', and the program finds 'Andorra'
msg.channel.send('you entered **' + country + '** and the checking was **' + arr1[country] + '** [Using arr1]');
}
else if(arr2[country]){ //checking if user inputted country name
//EXECUTE on found unitedkingdom to UK - add .tolowerCase
msg.channel.send('you entered **' + country + '** and the checking was **' + arr2[country] + '** [Using arr2]');
} else {
msg.channel.send('else statement executed');
}
}
At the moment, this always prints ('else statement executed').
Any help would be appreciated - thank you!
You tries check for arr1[country]
, where country
can be in different case, like arr1['uk']
, when UK
-key in your object in upper case.
Make something like this:
let arr1 = {
"AD":"Andorra",
"AE":"U A E",
}
// remove spaces from value, make it upper case and reverse
const makeObj = ([key, value]) => {
// array [VALUE, key]
let result = [value.toUpperCase().replace(/\s/g, ''), key];
return result;
}
let arr2 = Object.fromEntries(Object.entries(arr1).map(makeObj)); // reverse object
arr1 = Object.assign(arr1, arr2); // make single object
let country = msg.content.split('.')[1].toUpperCase(); // requested country in upper case
if(arr1[country]){
msg.channel.send(country, arr1[country]);
} else {
msg.channel.send('else statement executed');
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments