谁来看看怎么回事,关于javascript的,求改进,去重复

sun342115450 2011-08-06 06:09:23
<script language="javascript">
var arr=new Array(2,3,23,32,32,23,56,73);
var temp;
document.write("去掉重复的数以前:")
document.write(arr)
for(var i=0;i<arr.length-1;i++)
{
for(var j=i+1;i<arr.length;j++)
{
if(arr[i]==arr[j]){
temp=arr[length-1];
arr[length-1]=arr[i];
arr[i]=temp;

}
arr.pop( );
}
}
document.write("去掉重复的数以后:")
document.write(arr);
</script>
...全文
84 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
打字员 2011-08-06
  • 打赏
  • 举报
回复

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();
MuBeiBei 2011-08-06
  • 打赏
  • 举报
回复
<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>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧