使用迭代器使用标志获取数组中的下一项



我正在尝试获取对象对象中的下一个对象(您是否了解我的漂移)。我正在浏览歌曲列表,并试图确定要播放的下一首歌曲。我使用标志playing来检查歌曲是否正在播放,然后我获取下一首歌曲的索引并将 id 添加到名为 song 的 var 中

    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;

下面是对象的外观:

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

问题是,我的代码只播放播放列表中的前两首歌曲。为什么会这样,或者有更好的方法来实现这一目标?

谢谢

附言:这里的函数在JSFIDDLE上:http://jsfiddle.net/h1m7bx8g/

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

输出

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

最新更新