Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!