删除数组中重复项(uniq)
代码
TUI.unique = function( array ) {
var ret = [], record = {}, it, tmp;
var type = {
“number”: function(n){ return “_TUI_num” + n; },
“string”: function(n){ return n; },
“boolean”: function(n){ return “_TUI_boolean” + n; },
“object”: function(n){ return n === null ? “TUI_null” : $.data(n); },
“undefined”: function(n){ return “_TUI_undefined”; }
};
for ( var i = 0, length = array.length; i < length; i++ ) {
it = tmp = array[i];
tmp = type[typeof it](it);
if( !record[tmp] ) {
ret.push(it);
record[tmp] = true;
}
}
return ret;
};
测试:
var b=[1,3,5];
TUI.unique([1,3,4,5,null,false,$(".pack")[0],b,"ab","cc",[1,3],3,6,b,1,false,null,"null","","false","",$(".pack")[0],"cc"])
var ret = [], record = {}, it, tmp;
var type = {
“number”: function(n){ return “_TUI_num” + n; },
“string”: function(n){ return n; },
“boolean”: function(n){ return “_TUI_boolean” + n; },
“object”: function(n){ return n === null ? “TUI_null” : $.data(n); },
“undefined”: function(n){ return “_TUI_undefined”; }
};
for ( var i = 0, length = array.length; i < length; i++ ) {
it = tmp = array[i];
tmp = type[typeof it](it);
if( !record[tmp] ) {
ret.push(it);
record[tmp] = true;
}
}
return ret;
};
测试:
var b=[1,3,5];
TUI.unique([1,3,4,5,null,false,$(".pack")[0],b,"ab","cc",[1,3],3,6,b,1,false,null,"null","","false","",$(".pack")[0],"cc"])
var uniq = function (arr) {
var a = [],
o = {},
i,
v,
len = arr.length;
if (len < 2) {
return arr;
}
for (i = 0; i < len; i++) {
v = arr[i];
if (o[v] !== 1) {
a.push(v);
o[v] = 1;
}
}
return a;
}
var a = [],
o = {},
i,
v,
len = arr.length;
if (len < 2) {
return arr;
}
for (i = 0; i < len; i++) {
v = arr[i];
if (o[v] !== 1) {
a.push(v);
o[v] = 1;
}
}
return a;
}