可能重复:
从数组中删除重复项
你能帮帮我吗?我有这段代码..
<html>
<head>
<title>test</title>
<script>
var array = new Array();
function insert(val){
array[array.length]=val;
}
function show() {
var string="";
for(i = 0; i < array.length; i++) {
string =string+array[i]+' , ';
}
if(array.length > 0)
document.getElementById('result').innerHTML = string;
}
function removeDuplicateElement(arrayName)
{
var newArray=new Array();
label:for(var i=0; i<arrayName.length;i++ )
{
for(var j=0; j<newArray.length;j++ )
{
if(newArray[j]==arrayName[i])
continue label;
}
newArray[newArray.length] = arrayName[i];
}
return newArray;
document.getElementById('result1').innerHTML = newArray;
}
</script>
</head>
<body>
<h2>Demo</h2>
<form name="form1">
<input id="userinput" type="text" name="Input"/>
<input type="button" Value="Add" onclick="insert(getElementById('userinput').value),show();"/>
<input type="button" Value="Substract" onclick="removeDuplicateElement(array);"/>
</form>
<textarea style="border:1px solid #B3B3B3; width:218px;" rows="2" cols="30" type="text" id="result"></textarea>
<textarea style="border:1px solid #B3B3B3; width:218px;" rows="2" cols="30" type="text" id="result1"></textarea>
</body>
</html>
我试图实现我在互联网上找到的删除重复功能,但没有成功。(见下文)我做错了什么?
function removeDuplicateElement(arrayName)
{
var newArray=new Array();
label:for(var i=0; i<arrayName.length;i++ )
{
for(var j=0; j<newArray.length;j++ )
{
if(newArray[j]==arrayName[i])
continue label;
}
newArray[newArray.length] = arrayName[i];
}
return newArray;
}
谢谢!