在jquery中每个循环来自json对象How(make循环从对象中查找值并与另一个对象值进行比较以不重复打印)?

时间:2013-09-04 15:20:03

标签: javascript ajax json jquery

大家好我正在开发一个音乐播放器,我正在使用Json存储我的ID3或AKA“MP3元数据”。我遇到的问题是我正在提取一个对象和traingf以查看我是否可以比较该值,如果两个对象具有相同的值,则不要重复打印。如果我只是希望艺术家有一次不是2到无限重复

  • 这是JSON File示例;

    [
    {   
        "Artist": "",
        "Album": "",
        "Year":"",
        "Genre": "", 
        "Song": "",
        "Location": "",
        "Track": "",
        "Img": "",
        "Composer":""
    },
        {   
        "Artist": "The Rolling Stones",
        "Album": "Hot Rocks (1964-1971)",
        "Year":"2002",
        "Genre": "Rock", 
        "Song": "Heart of Stone",
        "Location": "scr/music/TheRollingStones/HeartofStone.mp3",
        "Track": "2",
        "Img": "scr/music/TheRollingStones/img/HotRocks.jpg",
        "Record":"Sony Music"
    },
        {   
        "Artist": "The Rolling Stones",
        "Album": "Hot Rocks (1964-1971)",
        "Year":"2002",
        "Genre": "Rock", 
        "Song": "Sympathy For The Devil",
        "Location": "scr/music/TheRollingStones/SympathyForTheDevil.mp3",
        "Track": "15",
        "Img": "scr/music/TheRollingStones/img/HotRocks.jpg",
        "Record":"Sony Music"
    },
    
            {   
        "Artist": "Led Zeppelin",
        "Album": "The Complete Led Zeppelin",
        "Year":"2007",
        "Genre": "Rock", 
        "Song": "Good Times Bad Times",
        "Location": "scr/music/LedZeppelin/GoodTimesBadTimes.mp3",
        "Track": "1",
        "Img": "scr/music/LedZeppelin/img/The Complete Led Zeppelin.jpg",
        "Record":"Atlantic Records"
    }
    
    ]
    

    Javascript文档

        $.getJSON('scr/json/musicdata.json', function(data){
            var output = '<ol>';
                 data.sort(function(a, b){
                 return [a.Artist] < [b.Artist] ?  0 : 1;
             });//End of Sort by Artist 
    
             $.each(data, function(key,val) {
    
             if(val.Artist != ""){
    
             if(PreArtis != val.Artist){
    
            output += '<li><a class="songname" href="#" data-src="'+ val.Location +'">' + val.Artist + '</a></li>';
                var PreSong = val.Artist ;
             }//End of PreSong != Song
            }//End of val.Artist     
            });//End of Each 
             output += '</ol>';
            $("#wrapper").append(output);
            });//End of getJSON 
    

    HTML

     

    感谢你帮助推进

  • 1 个答案:

    答案 0 :(得分:0)

    尝试

    var songs = {};
    $.each(data, function(key,val) {
        if(val.Artist != ""){
            var key = val.Artist + '@' + val.Album;
            if(!songs[key]){
                output += '<li><a class="songname" href="#" data-src="'+ val.Location +'">' + val.Artist + '</a></li>';
                songs[key] = true ;
            }//End of PreSong != Song
        }//End of val.Artist     
    });//End of Each 
    output += '</ol>';
    $("#wrapper").append(output);
    

    演示:Fiddle

    您可以更改key以查找唯一组合。例如:如果您想要艺术家,专辑和歌曲的独特组合,请使用var key = val.Artist + '@' + val.Album + val.Song;