3
 var g_Year = [
         { Year: "2011" },
         { Year: "2011" },
         { Year: "2010" },
         { Year: "2012" },
         { Year: "2011" },
         { Year: "2012" }
];
fxnDrop = function () {
        $.each(g_Year , function (index, Ele) {
            var iYear = g_Year [index].Year,
                sHtmlYear = '<option value="' + iYear + '">' + iYear + '</option>';
            $("#DropDown_Year").each(function (index, Ele) {
                $("#DropDown_Year").append(sHtmlYear);
                if (Ele.value != iYear) {
                    $("#DropDown_Year").remove(sHtmlYear);
                }
            });
        });
}

在这里我有多年,我的目标是在下拉框中仅显示一次特定年份。例如:2011 必须在下拉框中仅显示一次。任何人都可以帮我做到这一点。我在 Dropbox 中获得了多个 2011 年的值。

4

4 回答 4

2

试试这个代码

var fxnDrop = function() {
    var yearsArray = [];
    $.each(g_Year, function(index) { 
        if($.inArray(+g_Year[index].Year , yearsArray) == -1){
            yearsArray.push(+g_Year[index].Year);
        }
    });
    yearsArray.sort();
    var sHtmlYear = '';
    $.each(yearsArray , function(i){
        sHtmlYear += '<option value="'+ yearsArray[i] +'">' 
                         + yearsArray[i]  + '</option>'; 
    });
     $("#DropDown_Year").append(sHtmlYear);
};

fxnDrop(); ​

检查小提琴

于 2012-11-08T07:22:10.117 回答
0
var g_Year = [
         { Year: "2011" },
         { Year: "2011" },
         { Year: "2010" },
         { Year: "2012" },
         { Year: "2011" },
         { Year: "2012" }
];
g_Year.sort(function(o1, o2){return o1.Year > o2.Year} );

// use reduce function
var uniqueG_Year = g_Year.reduce(function(previousValue, currentValue, index, array){
  if(previousValue.length == 0 || previousValue[previousValue.length-1].Year != currentValue.Year)
    previousValue.push(currentValue);
  return previousValue;
}, []);

// or simply use filter
var uniqueG_YearFiltered = g_Year.filter(function(element, index, array){
  return (index == 0 || array[index-1].Year != element.Year)
});
于 2012-11-08T07:51:53.883 回答
0

您可以使用Jquery.inArray()功能:对于 exe:-

  var categories=[];
         if ($.inArray(value.category, categories)==-1){
        categories.push(value.category);
        }

categories将包含您需要的独特类别已
更新
[SEE LIVE DEMO][2]

于 2012-11-08T07:11:01.380 回答
0

试试这个.. @pranav 提到的使用 Jquery.inArray()

var yeararray= [];

fxnDrop = function () {
    $.each(g_Year , function (index, Ele) {
       if ($.inArray(Ele.Year, yeararray)==-1) {
        var iYear = g_Year [index].Year,
        sHtmlYear = '<option value="' + iYear + '">' + iYear + '</option>';
        $("#DropDown_Year").each(function (index, Ele) {
            $("#DropDown_Year").append(sHtmlYear);
            if (Ele.value != iYear) {
                $("#DropDown_Year").remove(sHtmlYear);
            }
        });
          yeararray.push(Ele.Year);
       }
    });
}
于 2012-11-08T07:24:48.317 回答