0

Here is my example data: http://api.setlist.fm/rest/0.1/setlist/4bf763f6.json

I'm just writing a node app that prints out the details of this page. What I'm concerned with are sets, set and song.

var sets = setlist.sets
res.write(JSON.stringify(sets)) // THIS SHOWS THE CORRECT DATA
var numSets = Object.keys(sets).length;
for(var i = 0; i < numSets; i++){
    res.write("\nsets " + i);
    var set = sets.set[i];
    console.log(Object.getOwnPropertyNames(set))
    var numSet = Object.keys(set).length;
    res.write(JSON.stringify(set))
    for(var j = 0; j < numSet; j++){
        res.write("\nset " + (j+1) + " of " + numSet);
        var song = set.song;
        console.log(Object.getOwnPropertyNames(song))
        numSong = Object.keys(song).length;
        for(var k = 0; k < numSong; k++){
            res.write("\n song " + j + "-" + k);
            res.write("\n     "+JSON.stringify(song[k]["@name"]));
        }
    }
}

what I get is:

set 1 of 1
 song 0-0
     "Lift Me Up"
 song 0-1
     "Hard to See"
 song 0-2
     "Never Enough"
 song 0-3
     "Got Your Six"
 song 0-4
     "Bad Company"
 song 0-5
     "Jekyll and Hyde"
 song 0-6
     "Drum Solo"
 song 0-7
     "Burn MF"
 song 0-8
     "Wrong Side of Heaven"
 song 0-9
     "Battle Born"
 song 0-10
     "Coming Down"
 song 0-11
     "Here to Die"

There are TWO song elements in set: (sorry no code block or it won't wrap)

{
    "set": [{
        "song": [{
            "@name": "Lift Me Up"
        }, {
            "@name": "Hard to See"
        }, {
            "@name": "Never Enough"
        }, {
            "@name": "Got Your Six"
        }, {
            "@name": "Bad Company",
            "cover": {
                "@disambiguation": "British blues-rock supergroup",
                "@mbid": "0053dbd9-bfbc-4e38-9f08-66a27d914c38",
                "@name": "Bad Company",
                "@sortName": "Bad Company",
                "@tmid": "734487",
                "url": "http://www.setlist.fm/setlists/bad-company-3bd6b8b0.html"
            }
        }, {
            "@name": "Jekyll and Hyde"
        }, {
            "@name": "Drum Solo"
        }, {
            "@name": "Burn MF"
        }, {
            "@name": "Wrong Side of Heaven",
            "info": "Acoustic"
        }, {
            "@name": "Battle Born",
            "info": "Acoustic and Electric"
        }, {
            "@name": "Coming Down"
        }, {
            "@name": "Here to Die"
        }]
    }, {
        "@encore": "1",
        "song": [{
            "@name": "Under and Over It"
        }, {
            "@name": "Burn It Down"
        }, {
            "@name": "The Bleeding"
        }]
    }]
}

In Swift I just make set a Dictionary and it works just fine. Javascript is not my forte. Why can't I get that second song element?

0

2 Answers 2

2

setlist.set is an array of two objects, one containing the "regular"(?) songs and the other containing the encore information.

It looks like you are mixing up loop variables with other objects/arrays and not iterating what you think you're iterating.

Here's a simplified version that should show what you're expecting:

// `sets` is an object containing (at least) `set` and `url` properties
var sets = setlist.sets;

// `set` is an array containing (in your example, two) objects
var set = sets.set;

for (var i = 0; i < set.length; ++i) {
  console.log('Set %d/%d', i+1, set.length);

  // `curSet` is an object containing a `song` property
  var curSet = set[i];

  // `songs` is an array of objects
  var songs = curSet.song;

  for (var j = 0; j < songs.length; ++j) {
    // `song` is an object containing properties like `@name` and possibly others
    var song = songs[j];

    console.log(' - Song: %s', song['@name']);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect thanks. Sometimes its hard to bounce back and forth between languages. Just a little typo, should be songs[j] instead of songs[i] - no biggie. Thanks again!
1

The line

var numSets = Object.keys(sets).length;

Should be

var numSets = sets.set.length;

May I also suggest that you use a forEach loop rather than a for loop (a .map() would be even better). for loops are much more prone to bugs than the alternatives.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.