Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda