Home > Blockchain >  How to ignore undefined input from array table
How to ignore undefined input from array table

Time:12-04

I have bot code to input some text then show specific data base on that input.

But when I input data that is not defined in array, my bot crashed. How to ignore that wrong input?

const arrayTable = {
        array1 : ['A1', 'A2'],
        array2 : ['B1', 'B2']
//there are array 3 and more
};

function selectedData(input) {
        return arrayTable[input];
    }

selectedData("array1"); // Output A1 A2
selectedData("wronginput"); // Crash. I want this input ignored
selectedData("wronginput"); I want this input ignored

CodePudding user response:

You can check if the arrayTable contains value. If it contains, only then set the value.

...

function selectedData(input) {
        return arrayTable[input] ? arrayTable[input] : false ; // if input doesn't exists, it will return false
    }
  • Related