87,910
社区成员
发帖
与我相关
我的任务
分享
Array.prototype.unique = function() {
var temp = this.slice(0).sort();
var index = 0;
while(index < temp.length - 1) {
if(temp[index] == temp[index + 1]) {
splice(index, 1);
} else {
index += 1;
}
}
return temp;
}
var arr=new Array(2,3,23,32,32,23,56,73);
arr = arr.unique();
<script language="javascript">
var arr=new Array(2,3,23,32,32,23,56,73);
var temp;
document.write("去掉重复的数以前:")
document.write(arr)
Array.prototype.unique = function()
{
var a = {};
for(var i=0; i<this.length; i++)
{
if(typeof a[this[i]] == "undefined")
a[this[i]] = 1;
}
this.length = 0;
for(var i in a)
this[this.length] = i;
return this;
}
arr.unique()
document.write("去掉重复的数以后:")
document.write(arr);
</script>