Posts Tagged ‘remove’

Javascript: Remove values from array prototype

Wednesday, October 29th, 2008

In order to remove certain values or objects from an array many people iterate through it and remove unwanted occurences with the splice() method. It does what you want it to do but it’s pretty cumbersome. There are far easier and simpler ways to remove a certain value from an array.

If we have an array like so: arr = [1,2,2,3,4,5,2,6] and we’d do this: arr.remove(2) we’ll end up with [1,3,4,5,6]. Nobody likes two’s anyway.

Here is how I do it:

Array.prototype.remove = function (subject) {
	var r = new Array();
	for(var i = 0, n = this.length; i < n; i++)
	{
		if(!(this[i]==subject))
		{
			r[r.length] = this[i];
		}
	}
	return r;
}

Why is this better?

  • No copy of the original is needed to do the math
  • It is yet again inherently independent of other array prototypes or methods
  • It runs alot faster ;)

So there you go, another mystery solved.
Take care!

Javascript: Remove duplicates from Array

Friday, December 21st, 2007

Removing duplicate entries or values from a Javascript array is something which isn’t accomodated for by the native functions in Javascript. I searched Google for a few solutions but they were all lacking something in my opinion, be it performance or just sheer elegance.

*UPDATE*

There is now an array prototype function for removing duplicates. I suggest you use the prototype function instead of the functions below. It is the way it should have been done in the first place and is faster and moreover the most correct way to do it.

Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

You can now utilize the unique function like this:

var arr = [1,2,2,3,3,4,5,6,2,3,7,8,5,9];
var unique = arr.unique();
alert(unique);

The result will be [1,2,3,4,5,6,7,8,9].

*UPDATE*

I therefore made two variants myself:

The first one does what you’ll expect, it’ll regard the first encountered entry as the original and all subsequent entries as duplicates.

function unique(a)
{
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++)
   {
      for(var x = 0, y = r.length; x < y; x++)
      {
         if(r[x]==a[i]) continue o;
      }
      r[r.length] = a[i];
   }
   return r;
}

If we pass the following array [1, 2, 3, 1, 4, 5] to the function the result will be [1, 2, 3, 4, 5].

The second variant returns different results. It will regard the last encountered duplicate as the original. This may be desirable in certain situations.

function unique(a)
{
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++) {
      for(var x = i + 1 ; x < n; x++)
      {
         if(a[x]==a[i]) continue o;
      }
      r[r.length] = a[i];
   }
   return r;
}

The output in this case will be [2, 3, 1, 4, 5].

I wrote both these functions with performance in mind. I have thoroughly tested and profiled both of them. Here are the benefits over other solutions to the problem:

  • it’s just one function
  • the loop length is determined through the object model just once per loop (see the for statements: var i = 0, n = a.length;)
  • after a duplicate has been detected in the nested loop it doesn’t iterate further but goes on to the next entry in the source array (see continue statement)

You can easily adapt the function to make it a prototype function of an Array object. If people don’t know how to do it just ask, i’ll add it.

Hope this helps someone out!