Javascript Data Structures - A Collection Object
Written by Ian Elliot   
Thursday, 12 August 2021
Article Index
Javascript Data Structures - A Collection Object
A Collection

A Collection object

A standard associative array is an idea starting place for a collection implementation. A collection can be though of as a key value store that keeps track of the data you add to it and has some basic functions to add and remove data. This is almost what an associative array does so it makes sense to start from this augmented by some appropriate methods. 

Let's take the approach of implementing a constructor function that generates a collection object with the methods we need.  The data can be stored internally in an associative array.

The new Collection object has a count property and a collection property.

var Collection=function()

The collection property is the internal associative array that we are going to use to store the key,value pairs. It could be made private using any of the standard techniques based on of closure if you want to restrict access to it.

Adding an item

The first method we need is Add(key,item) which adds a new item under the associated key. With all collections you have to decide if you are going to allow duplicate keys but in most cases its is simpler and more logical to add the restriction that all keys are unique:

                             return undefined;
 return ++this.count

This add method returns the current count if the item has been added and undefined if the key already exists in the collection. notice that the count is incremented in the return statement. 

Remove method

A remove method is just as easy to create but in this case we need to make sure that there is actually a key within the collection to be removed:

                        return undefined;
 delete this.collection[key]
 return --this.count

Once again the method either returns the current count of items after the removal or undefined if no item was removed. You can modify this to return the count, no matter what happens.

Notice that way that the add and remove methods automatically keep a count of the number of items in the collection. Also notice that as count is directly accessible this scheme can be defeated. If you want to protect the count then make it private using closure and provide a getCount method.

Item key

Most collection objects also have something like an item(key) method that returns the item corresponding to the key specified. This too is easy to write:

 return this.collection[key];

Notice that if the key isn't in the collection the returned value is undefined.

Using the Collection

With these methods you can use the collection for useful things but in most cases you would probably define additional methods.

For example a removeAll could be used to clear the collection. You could extend the definition of item to be item(key,item) and allow the user to change the item associated with the key - so creating a mutable collection.

For example:

var myCol=new Collection();

This creates a new collection and adds three items. The alert displays 3.

To see the value associated with any key you could use:


which would display 1.

To remove an item:


Now the first alert will show undefined and the count will show 2.

Notice that while the examples use simple values for the key and the value they can in practice be more complicated. For example the key can be any string and the value can be any object. 









Last Updated ( Friday, 13 August 2021 )