1

新手来了 我编写了一个简单的测试函数,它遍历一堆歌曲列表,并为每个列表尝试从 Last.fm API 获取专辑封面图像。如果它不起作用,它应该在控制台中显示曲目和艺术家姓名。但是,当我运行它时,它总是显示相同的艺术家/曲目对,即最后一个迭代的,而不是触发错误的特定的。我不确定发生了什么事。

$(document).ready(function(){
array = []
for (var item in billboard) {
  var track = billboard[item]['song'];
  var artist = billboard[item]['artist'];
  $.getJSON("http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=myAPIkey&artist=" + artist + "&track=" + track + "&format=json&callback=?", function(data) {
      try{
        image = data.track.album.image[2]['#text'];
      }
      catch (err) {
        console.log(track + " - " artist);
        }
      });
    }
});


 var billboard = {

 "7-12-1986": {"artist": "Simply Red", "song": "Holding Back The Years"},
"6-28-1986": {"artist": "Patti LaBelle ", "song": "On My Own"},
 "7-5-1986": {"artist": "Billy Ocean", "song": "There'll Be Sad Songs (To Make You Cry)"},
 .....
 }
4

1 回答 1

0

看看for 和 for in的区别

我认为在这种情况下,您应该使用标准 for 循环。

于 2013-07-29T23:43:45.660 回答