举例说明:
var fruits=["apple","banana","pear","orange","banana","pear"];
$.unique(fruits);
返回结果:"apple","banana","pear","orange"。
如果需要排序,可以排序后在移除重复:
fruits.sort(fruits);//默认按字母排序
$.unique(fruits);
返回结果:"apple","banana","orange","pear"。
$.unique()在IE中是不可用的,同等的====兼容IE的方法:
//still sort the array
fruit.sort();
//$.unique(fruits);
fruits=uniqueArray(fruits);
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(var j=0; j<a.length; j++){
if(a[j]==e){
return true;
}
return false;
}
}