简体   繁体   中英

In jquery each Loop from json object How (make loop find a value from object and compare to another object value to not repeat a print)?

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

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

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM