const cities = [
{
id: 0,
city: "Buenos Aires",
country: "Argentina",
codeIATA: "AEP",
aeroName: "Aeroparque Internacional Jorge Newbery",
coordX: 15,
coordY: 36,
},
{
id: 1,
city: "El Calafate",
country: "Argentina",
codeIATA: "FTE",
aeroName: "Aeropuerto Internacional Comandante Armando Tola de El Calafate",
coordX: -49,
coordY: -63,
},
]
let origin = parseInt(prompt("ID Origen"));
let destination = parseInt(prompt("ID Destino"));
Inside an array I have multiple objects like the ones above. The user will select one of the objects for origin and the other for destination by entering 0 or 1 or whichever ID of objects that I will have. I'm guessing I'll have to match the number with the index or id inside the object to select it.
Once the object is selected for origin and destination I want to select the coordX and coordY of each and calculate the distance between each point (I already have the formula for that). I know how to do the math but I'm struggling to fetch the coordinates depending on which object the user selects.
CodePudding user response:
You can use cities.find
(documentation) to find the city object with the id
entered by the user:
const origin = 1;
const originCity = cities.find((city) => city.id === origin);