Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

291
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda