Return to Snippet

Revision: 37561
at December 12, 2010 04:48 by ronydee


Updated Code
Array.prototype.unique = function unique()
{
	var i = 0;
	while (i < this.length)
	{
		for (k = this.length; k > i; k--)
		{
			if (this[k] === this[i])
			{
				this.splice(k,1);
			}
		}
		i++;
	}
	return this;
}

var myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie', 'patty', 'damon', 'zach', 'joe','joe','joe','adam'];
alert(myarray.unique().join(', '));

Revision: 37560
at December 12, 2010 04:33 by ronydee


Initial Code
Array.prototype.unique = function unique()
{
	var i = 0;
	while (i < this.length)
	{
		var current = this[i];
		for (k = this.length; k > i; k--)
		{
			if (this[k] === current)
			{
				this.splice(k,1);
			}
		}
		i++;
	}
	return this;
}

var myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie', 'patty', 'damon', 'zach', 'joe','joe','joe','adam'];
alert(myarray.unique().join(', '));

Initial URL

                                

Initial Description
This snippet is based on this snippet: http://snipplr.com/view/45323/remove-duplicate-values-from-array/
The difference is that this function is an extension of the Array object and that it cleans the duplicates from the input Array instead of creating a new array with the unique values.

Initial Title
Remove Duplicate Values from Array 2

Initial Tags

                                

Initial Language
JavaScript