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

166
Visualizações
Getting an Array out of a nested fetch function

so im fetching some data from the PUBG Api, and pushing the needed data into an array called "playerRecentStats", but whenever i try to take this array out of the function i get an undefined or empty result, i can read it from some points on the function but whenever i try to read it out of the fetch or general function it doesnt work.

Here is the code for context

let playerRecentStats = [];
    function recentMatchesPerformance() {
      
      last10Matches.map((match) => {
        fetch(
          `https://api.pubg.com/shards/${playerPlatform}/matches/${match.id}`,
          options
        )
          .then((response) => response.json())
          .then((data) => {
            data.included.forEach((player) => {
              if (player.type == 'participant') {
                if (player.attributes.stats.name == playerName) {
                  playerRecentStats.push({
                    kills: player.attributes.stats.kills,
                    dmgDone: player.attributes.stats.damageDealt,
                    knocks: player.attributes.stats.DBNOs,
                    date: data.data.attributes.createdAt,
                  });
                }
              }
              // console.log(playerRecentStats); This reads here!
            });
            // console.log(playerRecentStats); This does not read here!
          });
      });
      return playerRecentStats; // This is returning an empty array!
    }
    recentStats = recentMatchesPerformance();
    // I want to read the playerRecentStats Array here.
    console.log(playerRecentStats); // Empty Array
    console.log(recentStats); // Empty Array

Hopefully you can help me, thanks.

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