Home > Uncategorized > Collection object in Javascript

Collection object in Javascript

Often, you’d want to extend the classical Javascript array, and use a collection type object instead. This is a generic collection class that I’ve written:

var Collection=function()
{
// Number of elements in collection
this.count=0;
// The underlying collection
this.collection={};

// Add item to the collection with a named key
this.addWithKey=function(key,item)
{
if(this.collection[key]!=undefined)
return undefined;
this.collection[key]=item;
return ++this.count
}

// Add item to the collection with a GUID key
this.add=function(item)
{
return this.addWithKey(newGuid(),item);
}

// Remove item from the collection by it’s key
this.remove=function(key)
{
if(this.collection[key]==undefined)
return undefined;
delete this.collection[key]
return –this.count
}

// Return one item as specied by key
this.item=function(key)
{
return this.collection[key];
}

// Apply a function on each element in the collection
this.forEach=function(block)
{
for (key in this.collection)
{
if(this.collection.hasOwnProperty(key))
{
block(this.collection[key],key);
}
}
}

// Internal method to generate a Guid
function newGuid() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+”-“+S4()+”-“+S4()+”-“+S4()+”-“+S4()+S4()+S4());
}

// Saves the collection to localStorage
this.Save = function()
{
if (typeof JSON == “undefined”)
{
alert(“Include JSON2.js in order to save this collection”);
return;
}
if (typeof localStorage == “undefined”)
{
alert(“Browser does not support localStorage”);
return;
}
var strJson = JSON.stringify(this);
localStorage.setItem(‘collection’, strJson);
return strJson;
}

// Loads the collection from localStorage
this.Load = function()
{
if (typeof JSON == “undefined”)
{
alert(“Include JSON2.js in order to load this collection”);
return;
}
var strJson = localStorage.getItem(‘collection’);
if (strJson == null)
{
return false;
}
var loaded = JSON.parse(strJson);
this.count = loaded.count;
this.collection = loaded.collection;
return true;
}
return true;
}

Then typical usage would be:

<html>
<head>
<script language=”javascript” src=”collection.js”></script>
<script language=”javascript”>
function init()
{
var dt1 = new Date();
var dt2 = new Date(“October 13, 1975 11:13:00”);

var dates=new Collection();
dates.add(dt1);
dates.add(dt2);

var strHtml = “”;
dates.forEach(function(p,key){
strHtml += p + ” (” + key + “)<br>”;
});

var output = document.getElementById(“output”);
output.innerHTML = strHtml;
}
</script>
</head>
<body onload=”init()”>
<div id=”output”></div>
</body>
</html>

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: