JSON对象下拉列表中的值不同

时间:2021-07-08 20:09:38

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() {

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

}

2 个解决方案

#1


2  

$('#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;
});

FIDDLE

小提琴

#2


2  

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);
    }
}

#1


2  

$('#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;
});

FIDDLE

小提琴

#2


2  

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);
    }
}