How to escape duplicated value in Jquery each loop? -


i want removed duplicated value when used $.each in jquery . selection of item same value want remove value keep one.

function dropdown(data, selectorid) {      var selector = '<select  style="width: 400px;" id="results">';     if (data == '') {         selector += '<option value="0">no reference</option>';     } else {         selector += '<option value="0">choose reference</option>';         $.each(data, function (i, val) {             selector += '<option value=' + val.journal_id + '>' + val.reference + '</option>';         });     }     selector += '</select>';     $(selector).appendto(selectorid); } 

use array track items added.

function dropdown(data, selectorid) {     var journal_idarray = [];     var selector = '<select  style="width: 400px;" id="results">';     if (data == '') {         selector += '<option value="0">no reference</option>';     } else {         selector += '<option value="0">choose reference</option>';         $.each(data, function (i, val) {             if( journal_idarray.indexof(val.journal_id) == -1 ){                 selector += '<option value=' + val.journal_id + '>' + val.reference + '</option>';                 journal_idarray.push(val.journal_id);             }         });     }     selector += '</select>';     $(selector).appendto(selectorid); } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -