0

I'm kinda new to JavaScript and jQuery, but I'm learning while I'm making things. Now I'm working with the last.fm api for JS (https://github.com/fxb/javascript-last.fm-api), and I'm trying to get the first album of my list.

I fetch the data with following code:

lastfm.track.getInfo({track: activeMedia.title, artist: activeMedia.author}, {success: function(trackInfoResult){
// doing things with trackInfoResult
}, error: function(code, message){
    console.log("Error code: " + code);
}});

The output for this is in the following structure (found on http://www.last.fm/api/show/track.getInfo):

<track>
  <id>1019817</id>
  <name>Believe</name>
  <mbid/>
  <url>http://www.last.fm/music/Cher/_/Believe</url>
  <duration>240000</duration>
  <streamable fulltrack="1">1</streamable>
  <listeners>69572</listeners>
  <playcount>281445</playcount>
  <artist>
    <name>Cher</name>
    <mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
    <url>http://www.last.fm/music/Cher</url>
  </artist>
  <album position="1">
    <artist>Cher</artist>
    <title>Believe</title>
    <mbid>61bf0388-b8a9-48f4-81d1-7eb02706dfb0</mbid>
    <url>http://www.last.fm/music/Cher/Believe</url>
    <image size="small">http://userserve-ak.last.fm/serve/34/8674593.jpg</image>
    <image size="medium">http://userserve-ak.last.fm/serve/64/8674593.jpg</image>
    <image size="large">http://userserve-ak.last.fm/serve/126/8674593.jpg</image>
  </album>
  <toptags>
    <tag>
      <name>pop</name>
      <url>http://www.last.fm/tag/pop</url>
    </tag>
    ...
  </toptags>
  <wiki>
    <published>Sun, 27 Jul 2008 15:44:58 +0000</published>
    <summary>...</summary>
    <content>...</content>
  </wiki>
</track>

Getting my artist name, and id I can manage by the following code:

console.log(rackInfoResult.track.name);

But now I want to have the first album. How can I select the album with position="1"?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Kiwi
  • 2,713
  • 7
  • 44
  • 82

3 Answers3

1

Ok, I found it.

I used the following code to track down the right elements

for(var key in trackInfoResult.track) {
    var value = trackInfoResult.track[key];
    console.log(key + " : " + value);
}

and adding every time one of the keys behind the trackInfoResult.track

Kiwi
  • 2,713
  • 7
  • 44
  • 82
0

Then you should try to do ti this way. It worked for me .

in the success function of your script

    success: function(trackInfoResult){
     var x = eval('('+trackInfoResult+')');
         console.log(x);     
         console.log(x[0]);
    }

i think this should work for you also .

Tarun Kumar
  • 508
  • 3
  • 14
0

You could try:

var myAlbum = trackInfoResult.track.album;
if (parseInt(myAlbum.getAttribute('position'), 10) === 1) {
    // do stuff here
}
JamieJag
  • 1,541
  • 2
  • 11
  • 18