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
Post a Comment