jQuery .unique()的使用

var yearArray = new Array(2009, 2009, 2010, 2010, 2009, 2010);
$.unique(yearArray);
返回 2009, 2010, 2009, 2010
 
var yearArray = new Array(2009, 2009, 2010, 2010, 2009, 2010);
yearArray.sort();
$.unique(yearArray);
返回 2010, 2009
 
 
兼容ie的修改方式:
var yearArray = new Array(2009, 2009, 2010, 2010, 2009, 2010);
// still sort the array
yearArray.sort();
//$.unique(yearArray);
yearArray = uniqueArray(yearArray);

function uniqueArray(a){
    temp = new Array();
    for(var i = 0; i < a.length; i ++){
        if(!contains(temp, a[i])){
            temp.length+=1;
            temp[temp.length-1] = a[i];
        }
    }
    return temp;
}
function contains(a, e){
    for(j=0;j<a.length;j++)if(a[j]==e)return true;
    return false;
}

posted on 2010-11-13 18:18  linzheng  阅读(10529)  评论(0编辑  收藏  举报

导航