Get next item in array using iterator using flags

180 Views Asked by At

I am trying to obtain the next object in an object of objects (is you get my drift). I'm looking through the a list of songs, and trying to determine the next song to play. I use the flag playing to check if the song is being played, then i get the index of the next song and add the id to a var called song

    var stop = false;
    var song = null;
    $.each(playlist,function(index, value){
        if( !value.played ){
            if( stop ){
                song = index;
                return false;
            } else {
                if ( value.playing ) {
                    playlist[index].playing = false;
                    stop = true;
                }
            }
        }
    });
    playlist[song].playing = true;
    playlist[song].played= true;

here is what the object looks like:

{
 'hash1': {name:'test',played:true,playing:true},
 'hash2': {name:'test2',played:true,playing:true}
}

The problem is, my code only ever plays the first two songs in the playlist. Why is this, or is there a better way to achieve this?

Thanks

p.s. here the function is on jsfiddle: http://jsfiddle.net/h1m7bx8g/

1

There are 1 best solutions below

0
On BEST ANSWER
 $.each(playlist,function(index, value){
        if( !value.played ){
            if( stop ){
                song = index;
                return false;
            } else {
                if ( !value.playing ) {
                    playlist[index].playing = false;
                    stop = true;
                }
            }
        }
    });

outputs

(index):87 hash2
(index):87 hash3
(index):87 hash4
(index):87 hash5
(index):87 hash1