0

I'm trying to loop through the playedGames from an API. I need to place a new game between two teams in a new row in a table. I can only get the same game to be shown - and I can't figure out why. I put playedGames[i] since I hoped it would iterate throw all 13 games- but obviously not :-)

Here's the code:

        export function getMatchData() {

       return fetch('https://stryk.herokuapp.com/strycket2022')

    .then(function (response) {

      return response.json();

    })

    .then(function (data) {


      for (let i = 0; i < 12; i++) {

        return data.playedGames[i].teams[1].name + " VS " + data.playedGames[i].teams[2].name;

      }



    });
}
Ron
  • 5,900
  • 2
  • 20
  • 30
Lukas E
  • 47
  • 5
  • You're returning from the function after only reading one value. Don't return if you want to access each value. – Unmitigated Dec 20 '22 at 22:51
  • Maybe a silly question but how will I then write it correctly where will I return ( guess the function needs to return the information when I export the function) :-) ?? – Lukas E Dec 20 '22 at 22:54
  • Use the static and instance methods of [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods) instead, e.g. [`map`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map). – Sebastian Simon Dec 20 '22 at 22:54

0 Answers0