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

293
Vistas
Why isn't my Javascript array able to store JSON data?
$(document).ready(function() {
    var activeGames = [];

    $.getJSON('php/postActiveGames.php', {'index':99}, function(data) {
        $.each(data, function(index, user) {
            activeGames.push(user.username);
            console.log(activeGames[index]);
        });
    });

    console.log(activeGames[0]);
});

I'm trying to store data from a MYSQL database in a Javascript array. When I run console-log commands inside the callback function, the output is as I expect:

    Joel
    Scott
    Troy

But then when I try to log the array elements outside of the function, the console reads undefined. And interestingly, the last line of code seems to be executing first. Can anyone point out what I'm doing wrong?

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