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

187
Visualizações
How do I access variables from a nested arrow function?

I am storing the data from an API call in variables and I would like to use the data in the variables elsewhere in the function. However, when I printout what is in the variables outside of the arrow function, the data has a type of undefined.

The first console.log(coordinates); displays ["08002", "39.916258", "-75.021428"] and the second displays

[
    "08002",
    "39.916258",
    "-75.021428"
]

But when I have console.log(coordinates[1]); outside of the arrow function it says it's undefined.

const getLongLat = function(zip) { //gets long lat from a zip code
  zip = document.getElementById("ZipCode").value
  let lat = 0;
  let long = 0;
  let coordinates = [];
  if (zip == "") {
    zip = "08002"
  }
  coordinates.push(zip);
  const APIkey = "#########";
  let base = `http://open.mapquestapi.com/geocoding/v1/address? 
key=${APIkey}&location=${zip}`;
  console.log("base " + base);

  fetch(base)
    .then(response => response.json())
    .then(data => {

      //makes it so that the city is in the us
      let i = 0;
      while (data.results[0].locations[i].adminArea1 != "US") {
        i++
      }
      lat = String(data.results[0].locations[i].latLng.lat);
      long = String(data.results[0].locations[i].latLng.lng);
      coordinates.push(lat);
      coordinates.push(long);
      console.log(coordinates);

    })
  console.log(coordinates);
  return coordinates;
};
about 4 years ago · Juan Pablo Isaza
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