javascript - 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 developing music player , i'm using json store id3 or aka "mp3 metadata". problem i'm confronting i'm extracting object , traingf see if can compare value , if both object have same value don't not repeat print. in case want artist 1 time not 2 infinity repeat in
this 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 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 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 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 key find unique combination. ex: if want unique combination of artist, album , song use var key = val.artist + '@' + val.album + val.song;
Comments
Post a Comment