使用标志使用iterator获取数组中的下一项

时间:2015-06-14 19:29:02

标签: javascript jquery loops iterator iteration

我正在尝试获取对象对象中的下一个对象(你得到了我的漂移)。我正在查看歌曲列表,并尝试确定下一首要播放的歌曲。我使用标记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}
}

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

由于

P.S。这里的函数在jsfiddle上:http://jsfiddle.net/h1m7bx8g/

1 个答案:

答案 0 :(得分:0)

 $.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