0

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!

1
  • 1
    xcoordinate = xcoor[input]; Commented Jul 24, 2019 at 12:26

3 Answers 3

1

I would make a dictionary for the coordinates. Like this:

const coordinates = {
"London": {x: ..., y: ...},
"Paris": {x: ..., y: ...}
};

console.log('The coordinates for London: ',coordinates['London']);
console.log('The x coordinate for Paris: ', coordinates['Paris'].x);

If you want to use your input var to find the coordinates, you can do it like this:

const result = coordinates[input];
console.log(result.x, result.y) //returns the x and y coordinates for the user input
Sign up to request clarification or add additional context in comments.

Comments

1
var input = document.getElementById('input').value;
xcoordinate = xcoor[input.toLowerCase()];

With a map object, it will save alot of if else

var map = {
     'washington d.c.': 'washdc',
     'las vegas': 'lasvegas'
};
var input = document.getElementById('input').value;
xcoordinate = xcoor[map[input.toLowerCase()] || input.toLowerCase()];

2 Comments

Note that this doesn't work out of the box as "Washington D.C." should be stored in xcor["washdc"] not in xcor["washington d.c."], so you will need some kind of mapping.
Yes, he will need a map object anyway.
-1

I would like to solve it using the object as shown below,

var xcolor = {
london: 0.1278,
paris: 2.3522,
// keep adding more values here 

}

var ycolor = {
    london: 0.9,
    paris : 10
    // keep adding more values here 
}

let  coordinates = {
    xcoordinate : undefined,
    ycoordinate : undefined 
}



function setInput(input){

  if(xcolor.hasOwnProperty(input) && ycolor.hasOwnProperty(input)){
    coordinates.xcoordinate = xcolor[input];
    coordinates.ycoordinate = ycolor[input];
  }
}

setInput('paris');

console.log(coordinates.xcoordinate);
console.log(coordinates.ycoordinate);

setInput('london');
console.log(coordinates.xcoordinate);
console.log(coordinates.ycoordinate);

I hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.