Reputation: 543
I am trying to exttract distinct values into a drop down list using the following code but its not bringing in unique strings, can someone help please?
function build_refinesearch_cancer_combo() {
$('#combolist-cancer-type').empty();
var list = [];
var htmlResults = '<option value="-1" selected>Select cancer type_</option>';
for (var i = 0; i < user.length; i++) {
CancerID = user[i].FKCancerTypeID;
Cancer = user[i].Cancer;
if (Cancer != list) {
htmlResults += '<option value="' + CancerID + '">' + Cancer + '</option>';
list = Cancer;
}
}
$('#combolist-cancer-type').append(htmlResults);
}
Upvotes: 2
Views: 1556
Reputation: 318322
$('#combolist-cancer-type').html(function() {
var ret = '<option value="-1" selected>Select cancer type_</option>',
u = user.slice(),
arr = [];
(function get() {
if (u.length) {
var v = u.shift();
if ( $.inArray(v.FKCancerTypeID, arr) == -1 ) {
arr.push(v.FKCancerTypeID);
ret += '<option value="' + v.FKCancerTypeID + '">' + v.Cancer + '</option>';
}
get();
}
}());
return ret;
});
Upvotes: 2
Reputation: 36458
It seems like you want to keep a list of cancers already seen, and skip re-adding those.
If so, don't reassign the list each time. Check whether our new thing is in the list, and add to it if not.
var list = [];
var htmlResults = '<option value="-1" selected>Select cancer type_</option>';
for (var i = 0; i < user.length; i++) {
CancerID = user[i].FKCancerTypeID;
Cancer = user[i].Cancer;
if ($.inArray(Cancer, list) < 0) {
htmlResults += '<option value="' + CancerID + '">' + Cancer + '</option>';
list.push(Cancer);
}
}
Upvotes: 1