0

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

  • .

    这是 JSON 文件示例;

    [
    {   
        "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

    感谢您的帮助

  • 4

    1 回答 1

    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);
    

    演示:小提琴

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

    于 2013-09-04T15:31:27.460 回答