1

I am trying to build an array of figure strings this API returns to me using push(). However, when I console.log() the figures, it shows empty but when I open them, it says 0: "figure 1" 1: "figure 2" so they are there but how do I access them?

$(document).ready(function() {
  var pixels = ['.!!Britty!!.', 'Jaguarh'];
  var figures = [];

  pixels.forEach(function(p) {
    $.get('https://www.habbo.com/api/public/users', {
        name: p
      })
      .done(function(response) {
        figures.push(response.figureString);
      });
  });

  console.log(figures);
  
  $('#pixel').attr('src', 'https://www.habbo.com/habbo-imaging/avatarimage?figure=' + figures[figures.length - 1]);
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<img id="pixel" />
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • Here is the code rewritten using async/await, it will run in modern browsers. It consists of async immediately executing function. (async () => { const pixels = ['.!!Britty!!.', 'Jaguarh']; const figures = []; for (const pixel of pixels) { const response = await $.get('https://www.habbo.com/api/public/users', { name: pixel }); figures.push(response.figureString); } console.log(figures); })(); – user44 Oct 12 '18 at 20:43

0 Answers0