Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
How to use multiple properties of an object in an array depending on user input?
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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!