<lk******@geoci ties.com> wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
The PHP scripting language has the array_unique() function that gets
the unique, non-redundant values out of an array.
Does Javascript have anything similar?
I think these functions will do what you want.
..arrayUnique() returns a new array containing the unique values,
..trashDuplicat es() acts upon the original array, returning it with duplicates removed.
For strict type-comparisons, remove the comments in the 'if' statement.
<SCRIPT type='text/javascript'>
var dups=["1", 1, "four", "3", 2, "2", 1, "four", "2", 2, 3, "3",2];
if( typeof Array().arrayUn ique=='undefine d' )
Array.prototype .arrayUnique=fu nction()
{
var noDuplicates=[];
for( var i=0, k=0; i<this.length; i++ )
{
for( var j=0; j<noDuplicates. length && this[i]!=noDuplicates[j] ; j++ )
;
if(j==noDuplica tes.length)
noDuplicates[k++]=this[i];
}
return noDuplicates;
}
if( typeof Array().trashDu plicates=='unde fined' ) // as if...
Array.prototype .trashDuplicate s=function()
{
for( var i=0; i<this.length; i++ )
for( var j=0; j<this.length; j++ )
if( i!=j && this[i]==this[j] /** && typeof(this[i])==typeof(this[j]) **/ )
for( var k=j; k<this.length; k++ )
{
this[k]=this[k+1];
this.length--;
} // no .splice() in I.E. 5.0 :(
return this;
}
alert("dups.arr ayUnique()==[ "+dups.arrayUni que() +" ]\n\ndups.trashD uplicates()==["
+dups.trashDupl icates()+"] New length is: "+dups.leng th);
</SCRIPT>
--
Stephen Chalmers
http://makeashorterlink.com/?H3E82245A
547265617375726 520627572696564 206174204F2E532 E207265663A2054 51323437393134