从后面的 json 创建的 select 列表中删除重复项

Remove Duplicates from a select list created from a json in the back

大家好,请帮忙, 我有一个这样的 json...

{
    Result: "OK", 
    Options: [
    {
        DisplayText: "Απρίλιος",
        Value: "4"
    },
    {
        DisplayText: "Απρίλιος",
        Value: "4"
    },
    {
        DisplayText: "Απρίλιος",
        Value: "5"
    },
    {
        DisplayText: "Απρίλιος",
        Value: "5"
    },
    {
        DisplayText: "Αύγουστος",
        Value: "10"
    },
    {
        DisplayText: "Αύγουστος",
        Value: "10"
    },
    {
        DisplayText: "Αύγουστος",
        Value: "11"
    },
    {
        DisplayText: "Αύγουστος",
        Value: "11"
    }// And so on
}
 ... an so on...

您会注意到我在 Value 中有重复,但在 DisplayText 中也有重复,我只想像那样放入 select 列表

Result: "OK", 
Options: [
            {
                DisplayText: "Απρίλιος",
                Value: "4"
            },
            {
                DisplayText: "Αύγουστος",
                Value: "10"
            },

也就是说,如果 Value 与其他一些保持相同,如果 DisplayText 与其他一些保持相同,请不要关心我保持哪个值...

重复的问题只出现在最新的 IE 中,在 mozilla 或 chrome 中没有重复

所以我的代码 - 前面的函数就像使用 inArray 一样,但这是当我在 DisplayText 中有重复项时不起作用,因为只查找 Value

中的重复项
function loadDynamicDdl(ajaxUrl , ddlId) {
    $.ajax({
        dataType:'json',
        type: 'GET',
        url: ajaxUrl,
        cache:true,       
        async:false,
        success:function (response) {

            if (response.Result == 'ERROR') { 
                $('#system-message-container').html('<h2 style="color:red;">' + response.Message + '</h2>');
                $('#maincontent-container').hide();
            }

            var ddl = $('#' + ddlId);
            ddl.empty();            
            var opts = response.Options;         
            //Start checking for duplicates numbers in the Value field in json and remove them        
            var arr = [], //to collect number from Value field
            collection = []; //collect unique object from json
            $.each(opts, function (i, item) {
                if ($.inArray(item.Value, arr) == -1) { // it returns -1 when it doesn't find a match
                    arr.push(item.Value);//push id value in arr       
                    collection.push(item); //put object in collection to access it's all values       
                    ddl.append($("<option />").val(item.Value).text(item.DisplayText));
                }
            });
        }
    }
}

但这仅适用于 Value 中的重复项.... DisplayText 中的重复项呢? 请一些帮助的人,因为我尝试了很长时间来解决这个问题但没有运气,并且只有在最新的 IE 中才在 mozilla 或 chrome 中显示问题没问题......但我的客户在 IE 中工作:( , , , 提前谢谢你们..

您可以使用下面的代码从 json 数组中删除

function arrUnique(arr) {
    var cleaned = [];
    arr.forEach(function(itm) {
        var unique = true;
        cleaned.forEach(function(itm2) {
            if (_.isEqual(itm, itm2)) unique = false;
        });
        if (unique)  cleaned.push(itm);
    });
    return cleaned;
}

var jsonObj={
    Result: "OK",
    Options: [{
        DisplayText: "Απρίλιος",
        Value: "4"
    }, {
        DisplayText: "Απρίλιος",
        Value: "4"
    }, {
        DisplayText: "Απρίλιος",
        Value: "4"
    }, {
        DisplayText: "Απρίλιος",
        Value: "4"
    }, {
        DisplayText: "Αύγουστος",
        Value: "11"
    }, {
        DisplayText: "Αύγουστος",
        Value: "11"
    }, {
        DisplayText: "Αύγουστος",
        Value: "11"
    }, {
        DisplayText: "Αύγουστος",
        Value: "11"
    }]
};

jsonObj = arrUnique(jsnobject);

看到这个 JSFiddle,我尝试使用这个解决方案:

var result ={ Result: "OK", 
           Options: [
              {
             DisplayText: "ABC",
             Value: "4"
             },
             {
             DisplayText: "ABC",
              Value: "4"
            },
             {
             DisplayText: "ABC",
             Value: "5"
             },
             {
             DisplayText: "ABC",
              Value: "5"
            },
             {
             DisplayText: "XYZ",
             Value: "10"
             },
             {
             DisplayText: "XYZ",
             Value: "10"
            },  
             {
             DisplayText: "XYZ",
             Value: "11"
             },
             {
             DisplayText: "XYZ",
             Value: "11"
            },  
               ]
};
var finalResult = [];
var Values =[];
var DisplayTexts = [];

$.each(result.Options, function(index, value) {
    if ($.inArray(value.DisplayText, DisplayTexts)==-1
        && $.inArray(value.Value, Values)==-1     ) {
         Values.push(value.Value);
         DisplayTexts.push(value.DisplayText);
         finalResult.push(value);
    }
});

console.log(finalResult);

万一其他人需要它,并且在你们的大力帮助下,尤其是 111 就是这个,

       var opts = response.Options;
       var finalResult = [];
       var Values =[];
       var DisplayTexts = [];
       $.each(opts, function (i, value) {
       if ($.inArray(value.DisplayText, DisplayTexts) == -1 && ($.inArray(value.Value, Values) == -1)) { // it returns -1 when it doesn't find a match
      Values.push(value.Value);//push id value in arr   
      DisplayTexts.push(value.DisplayText);
      finalResult.push(value);      
      ddl.append($("<option />").val(value.Value).text(value.DisplayText));
      }
      });