How do I search for an element in a JS object (building a Discord Bot - discord.js)

user13566892

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!

TAB_mk

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I merge images in discord.js for a discord bot?

How Do I Auto Reconnect my Discord.js Bot

How do I make discord bot have an emoji reaction after it sends its message in discord.js?

Discord.js How do I get the bot to react to the message it just sent in discord by id?

discord bot using node.js - how do i convert a message to lowercase?

How do I code event/command handlers for my Discord.js bot?

discord.js question. how do i make the bot give a different response to one command?

How do I get my bot to wait for reply from a mentioned user discord.js?

How do i stop my discord.js bot from spamming the output

How Do i Check if a Certain Bot Exists in a Guild | discord.js

How do I make my Discord js Bot check if someone is typing in the bots dms?

How do I list all servers in a discord.js v12 bot

How can I deploy a discord.js bot to Cloud Functions?

How would I make a bot that "host events" with Discord.js?

Discord.js How would I create a bot status announcement?

How can I make discord.js bot detect an attachment

How can I fix this error in discord.js bot?

How do you search for an embed in discord.js

I want to make a discord music bot in js

How can I implement a !leaveguild <guildID> command (that leaves the specified guild) into my Discord (discord.js) bot?

How can I list the all Discord servers ID where my bot are in the console? | discord.js

how to save an image from discord using discord bot node js

How to dm a user with a discord.js bot

How do I schedule a task with in a discord bot

How do I make sort json object values in discord.js code block?

TypeError in Discord.JS-Commando Discord Bot

Hibernate Discord bot discord.js

Discord.js translate bot for discord server

error message in discord bot(discord.js)?