Re: i need an equivalent to PHP's array_unique function
lkrubner@geocit ies.com wrote:[color=blue]
> 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?[/color]
The short answer is no, but there's a few ways around it.
If you use an Object as a pretend associative array you can either
iterate once through the array and put each element into the
associative array and get a unique list (but it won't be guaranteed to
be in the same order as the indexed array, using the for..in
statement). You could also just use an "associativ e array" to start
with, instead of an array.
If you care about order, you could add the data to both an associative
array and the indexed array, and use the associative array to quickly
check if the data already exists before adding it to the arrays.
Re: i need an equivalent to PHP's array_unique function
Jc wrote:[color=blue]
> lkrubner@geocit ies.com wrote:
>[color=green]
>>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?[/color]
>
>
> The short answer is no, but there's a few ways around it.
>
> If you use an Object as a pretend associative array you can either
> iterate once through the array and put each element into the
> associative array and get a unique list (but it won't be guaranteed to
> be in the same order as the indexed array, using the for..in
> statement). You could also just use an "associativ e array" to start
> with, instead of an array.[/color]
The only problem being that JS has no "associativ e arrays".
Re: i need an equivalent to PHP's array_unique function
Randy Webb wrote:[color=blue]
> Jc wrote:[color=green]
> > If you use an Object as a pretend associative array you can either
> > iterate once through the array and put each element into the
> > associative array and get a unique list (but it won't be guaranteed to
> > be in the same order as the indexed array, using the for..in
> > statement). You could also just use an "associativ e array" to start
> > with, instead of an array.[/color]
>
> The only problem being that JS has no "associativ e arrays".[/color]
I agree that JS does not explicitly implement a data structure called
an "associativ e array". However, being such an expressive language, JS
objects can definitely be used as a collection of name/value pairs,
which I don't have a problem referring to informally as an associative
array. Not to mention that this is significantly more meaningful to
someone coming from a PHP background.
I thought it was obvious that in my previous post I was referring to a
feature in JS which is commonly referred to as an "associativ e array"
for simplicity in explanations. Notice how I said "pretend", and notice
I quoted the words "associativ e arrays", and notice how I said to use a
JS object, which is how JS implements what is known as an associative
array in other languages.
Just to clarify things, when I referred to using a JS object as a
pretend associative array, I was referring to the technique described
on the following site (someone may have a better link):
Re: i need an equivalent to PHP's array_unique function
<lkrubner@geoci ties.com> wrote in message news:1118537082 .131128.238160@ g14g2000cwa.goo glegroups.com.. .[color=blue]
>
> 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?
>[/color]
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.
Re: i need an equivalent to PHP's array_unique function
lkrubner@geocit ies.com wrote:[color=blue]
> 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?
>[/color]
works with indexed, associative, and "mixed" arrays:
Array.prototype .unique = function()
{
var mark = [];
for(var i in this)
{
// var indx = this[i]; --> if type does not matter
// create a unique index if type does matter
var indx = this[i] + "_" + typeof(this[i]);
Re: i need an equivalent to PHP's array_unique function
*** correction follows ***
<snip>[color=blue]
>
>
> Array.prototype .unique = function()
> {
>
> var mark = [];
>
> for(var i in this)
> {
>
> // var indx = this[i]; --> if type does not matter
>
>
> // create a unique index if type does matter
> var indx = this[i] + "_" + typeof(this[i]);
>
> if(mark[indx])
> delete this[i];
> else
> mark[indx] = this[i];
> }
>
> this.sort();
>
> // empty indexed entries are at the end of the array
> // shorten it [delete does not reduce array][/color]
if(this.length) {[color=blue]
>
> while(!this[this.length-1]) this.length--;[/color]
}[color=blue]
>
> }
>
>[/color]
[**IE did not start generating errors until it was restarted]
Re: i need an equivalent to PHP's array_unique function
"Jc" <google@weinric hs.com> wrote in message
news:1118552476 .934185.263950@ z14g2000cwz.goo glegroups.com.. .[color=blue]
> lkrubner@geocit ies.com wrote:[color=green]
>> 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?[/color]
>
> The short answer is no, but there's a few ways around it.
>
> If you use an Object as a pretend associative array you can either
> iterate once through the array and put each element into the
> associative array and get a unique list (but it won't be guaranteed to
> be in the same order as the indexed array, using the for..in
> statement). You could also just use an "associativ e array" to start
> with, instead of an array.
>
> If you care about order, you could add the data to both an associative
> array and the indexed array, and use the associative array to quickly
> check if the data already exists before adding it to the arrays.[/color]
He can implement something that works very similar to PHP's
array_unique() quite easily:
<script type="text/javascript">
// modifies the current Array
function array_unique(ar r) {
var existingItems = {};
var prefix = String(Math.ran dom() * 9e9);
var ii = 0;
while (ii < arr.length) {
if (existingItems[prefix + arr[ii]]) {
arr.splice(ii, 1);
} else {
existingItems[prefix + arr[ii]] = true;
++ii;
}
}
}
// returns a copy
function array_unique2(a rr) {
var newArray = [];
var existingItems = {};
var prefix = String(Math.ran dom() * 9e9);
for (var ii = 0; ii < arr.length; ++ii) {
if (!existingItems[prefix + arr[ii]]) {
newArray.push(a rr[ii]);
existingItems[prefix + arr[ii]] = true;
}
}
return newArray;
}
var a = [ 'one', 'two', 'three', 'two', 'one' ];
alert(array_uni que2(a));
array_unique(a) ;
alert(a);
</script>
--
Grant Wagner <gwagner@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Comment