I need help with a specific piece of code. It is kind of difficult to describe, but I'll try to do it the best I can. I need to check a specific attribute of an object based on user input. For example, if the user inputs 'London', I want to be able to access an attribute called 'xcoor.london'.
Before I have tried a lot of 'if' and 'else if', for example,
xcoordinate = xcoor.london;
ycoordinate = ycoor.london;
}else if(input == 'Paris'){
xcoordinate = xcoor.paris;
ycoordinate = ycoor.paris;
}else if(input == 'Washington D.C.'){
xcoordinate = xcoor.washdc;
ycoordinate = ycoor.washdc;
}
"But I have dozens of inputs and this takes a lot of code.
I am just revising this program to make it take up less space,
and this part is the major problem.
I want something like this:"
var xcoor = {
london: 0.1278,
paris: 2.3522,
...
}
var input = document.getElementById('input').value;
xcoordinate = xcoor.input;
That's pretty much all; any ideas are welcome and appreciated! Thanks in advance!
xcoordinate = xcoor[input];