Javascript Data Structures - a collection object
Javascript Data Structures - a collection object
Written by Ian Elliot   
Thursday, 22 September 2016
Article Index
Javascript Data Structures - a collection object



Now we come to the tricky problem of enumerating a collection. You clearly cannot just use for in on the collection object e.g.

for(index in myCol)

This works but it displays the properties and methods of myCol not the collection it contains.

One solution is to enumerate the collection property:

for (index in myCol.collection)

This has the advantage the the for in is used but the disadvantage that the user has to remember to use the collection property. It also doesn't work if you want the collection property to be inaccessible.

Perhaps the time has come to give up enumeration using for in and provide an enumeration method. There are ways to create very general enumeration methods that can be run on any object but there are advantages to creating custom enumeration methods for each type of data object you create. So let's add a ForEach method to the collection object:

   for (key in this.collection){

The basic idea is that the enumerator is passed a block of code i.e. a function that it will execute once for each item in the collection. The item in question is passed to the block of code as the first parameter.

The way that this works is that a standard for in loop is used to step though the properties of the collection. Just in case someone has added properties and methods to the prototype of the collection we check that each key corresponds to an own property i.e. not provided by a prototype object. In most cases this should be unnecessary but it is included to show how the approach could be made defensive. A better defence would be to make collection private.

Finally the block of code is called with the item as the first parameter. Notice that the code block doesn't run in the context of the collection object i.e. it can't make use of this and it can't be a method belonging to another object. This might seem restrictive but the idea is that the block of code is passed to the enumerator as if it was a lambda expression or a Ruby block. For example:


The function is called once with item set to each item in the collection. Notice that you don't have to name the first parameter item and the function can have as many lines as it needs.

You can modify the forEach to pass the item and key if that would be more useful and you can drop the restriction on using methods by changing the call to the block of code to,item) where object is that object that the method belongs to passed into the forEach.

An alternative approach is to implement an enumerator function which simply returns the next value. This involves keeping track of where you are in the enumeration. There are no obvious easy ways to do this in pre-ES2005 JavaScript. 

Once you have seen how a collection object can be implemented it is fairly easy to customise and extend to other collection subtypes. 



Chapter List 

  1. The Associative Array

  2. JavaScript Data Structures - the Array object

  3. JavaScript Data Structures - the String Object

  4. Speed dating - the art of the JavaScript Date object

  5. A collection object

  6. Javascript data structures - Stacks

  7. JavaScript Data Structures - The Linked List

  8. JavaScript data structures - a Lisp-like list

  9. Stacks, Queue and Deque

  10. Javascript data structures - The Binary Tree

  11. JavaScript Data Structures - Typed Arrays I

  12. JavaScript Data Structures - Typed Arrays II



To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.



jQuery UI and Auto-Complete Address Entry

jQuery UI has a little-known feature that makes interactive auto-complete very easy. In this hands-on tutorial we put it together with the cloud-based Global Express Entry address auto-complete servic [ ... ]

JQuery 3 - Reinventing Events

JavaScript is an event driven language - which means you can't avoid dealing with events. jQuery doesn't just provide a browse-independent way of working with events, it reinvents the event system. Wi [ ... ]

Other Articles



blog comments powered by Disqus








Last Updated ( Thursday, 27 October 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2017 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.