日期:2014-05-16  浏览次数:20489 次

关于去除数组中重复项的问题
<%  
snum_str= "1   1   2   3   4   2   4 "
%>
<script   language= "javascript "   type= "text/javascript ">
var   snum_str= " <%=   snum_str   %> ";
var   snum_arr=snum_str.split( "   ");
for(i=1;i <snum_arr.length;i++){
for(j=i+1;j <snum_arr.length;j++){
if(snum_arr(i)==snum_arr(j)){
snum_arr(j).pop();
}
}
}
</script>

这个是我编的去除asp数组中重复项的代码  
到了if(snum_arr(i)==snum_arr(j)){这一句提示缺少函数
<%  
snum_str= "1   1   2   3   4   2   4 "
snum_arr=split( "1   1   2   3   4   2   4 ", "   ")
for   i=0   to   ubound(snum_arr)
for   j=i+1   to   ubound(snum_arr)
if   snum_arr(i)=snum_arr(j)   then   snum_arr(j)= "   "
next
next  
%>
开始用asp的循环可以把重复项找出来,但无法直接删除,所以赋值空,但发现仍然无法去掉数组中值为空的项;js中有pop()函数,所以方js中做了,但if处提示有错

------解决方案--------------------
用Dictionary:
<%
snum_str= "1 1 2 3 4 2 4 "
snum_arr=split(snum_str, " ")
Set dic = CreateObject( "Scripting.Dictionary ")
For i=0 To UBound(snum_arr)
If Not dic.exists(snum_arr(i)) Then
dic.Add snum_arr(i), snum_arr(i)
End If
Next
arr =dic.Items()
Set dic = Nothing
For i=0 To UBound(arr)
Response.Write arr(i)
Next

%>