如何添加到数组并获取值

时间:2018-10-12 20:01:57

标签: javascript

我正在尝试构建一个数字字符串数组,该API使用push()返回给我。但是,当我console.log()数字时,它显示为空,但是当我打开它们时,它显示为0: "figure 1" 1: "figure 2",所以它们在那里,但是我该如何访问它们?

$(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" />

0 个答案:

没有答案