<lk******@geocities.com> wrote in message news:11**********************@g14g2000cwa.googlegr oups.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,
..trashDuplicates() 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().arrayUnique=='undefined' )
Array.prototype.arrayUnique=function()
{
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==noDuplicates.length)
noDuplicates[k++]=this[i];
}
return noDuplicates;
}
if( typeof Array().trashDuplicates=='undefined' ) // as if...
Array.prototype.trashDuplicates=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.arrayUnique()==[ "+dups.arrayUnique() +" ]\n\ndups.trashDuplicates()==["
+dups.trashDuplicates()+"] New length is: "+dups.length);
</SCRIPT>
--
Stephen Chalmers
http://makeashorterlink.com/?H3E82245A
547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134