Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi guys I am developing a music player and I'm using Json for store my ID3 or AKA "MP3 Metadata". The problem that I'm confronting is that I'm extracting an object and traingf to see if I can compare the value and if both object have the same value don't not repeat the print. In case I just want the artist one time not 2 to infinity repeat in

  • .

    This is JSON File example;

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

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

    Thanks for you help in advances

  • share|improve this question

    1 Answer

    Try

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

    Demo: Fiddle

    You can change the key to find the unique combination. Ex: if you want a unique combination of Artist, Album and Song then use var key = val.Artist + '@' + val.Album + val.Song;

    share|improve this answer

    Your Answer

     
    discard

    By posting your answer, you agree to the privacy policy and terms of service.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.