<html>
<title>
</title>
<head>
<script>
var ary = new Array("111","22","33","1111","334","33","22","111","33");
function distinct(a)
{
return /(/x0f[^/x0f]+/x0f)[/s/S]*?/1/g.test(a)?distinct(a.replace(/(/x0f[^/x0f]+/x0f)([/s/S]*?)(/1)/g,"$1$2")):a;
}
function test()
{
a=distinct("/x0f"+ary.join("/x0f/x0f") +"/x0f");
var tt=document.getElementById('txt1');
tt.value=a.substr(1,a.length-2).split("/x0f/x0f");
//document.write(a.substr(1,a.length-2).split("/x0f/x0f"));
}
function display()
{
var t=document.getElementById('txt');
var val="";
for(var i=0;i<ary.length;i++)
{
val+=ary[i]+" ";
}
t.value=""+val;
}
</script>
</head>
<body οnlοad="display()">
<form>
数组中的内容有: <input type="text" size="100" value="" id="txt"/><br/>
消除重复项后有:<input type="text" size="100" value="" id="txt1"/><br/>
<input type="button" οnclick="test()" value="测试消除数组中重复项"/>
</form>
</body>
</html>
另外一种写法更简洁
<SCRIPT LANGUAGE="JavaScript">
<!--
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;
}
var a = [1,2,4,12,5,3,5,4,8,5,2,8,8,1,5];
alert(a +"/n"+ a.unique());
//-->
</SCRIPT>
<title>
</title>
<head>
<script>
var ary = new Array("111","22","33","1111","334","33","22","111","33");
function distinct(a)
{
return /(/x0f[^/x0f]+/x0f)[/s/S]*?/1/g.test(a)?distinct(a.replace(/(/x0f[^/x0f]+/x0f)([/s/S]*?)(/1)/g,"$1$2")):a;
}
function test()
{
a=distinct("/x0f"+ary.join("/x0f/x0f") +"/x0f");
var tt=document.getElementById('txt1');
tt.value=a.substr(1,a.length-2).split("/x0f/x0f");
//document.write(a.substr(1,a.length-2).split("/x0f/x0f"));
}
function display()
{
var t=document.getElementById('txt');
var val="";
for(var i=0;i<ary.length;i++)
{
val+=ary[i]+" ";
}
t.value=""+val;
}
</script>
</head>
<body οnlοad="display()">
<form>
数组中的内容有: <input type="text" size="100" value="" id="txt"/><br/>
消除重复项后有:<input type="text" size="100" value="" id="txt1"/><br/>
<input type="button" οnclick="test()" value="测试消除数组中重复项"/>
</form>
</body>
</html>
另外一种写法更简洁
<SCRIPT LANGUAGE="JavaScript">
<!--
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;
}
var a = [1,2,4,12,5,3,5,4,8,5,2,8,8,1,5];
alert(a +"/n"+ a.unique());
//-->
</SCRIPT>