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
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 à 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