Javascript dictionary & getIds

Coming from a .net background I must admit to missing many of the features that the framework provides like Hastables, Dictionaries and Lists. I've just come up with a really simple dictionary class that I use to hold results from my id parsing routine.

Dictionary

The dictionary is a simple class that maintains an items object and has and add, remove and exists method that takes a parameter array and parses each items in pairs. First item is the key, second item is the value. The items are stored in a seperate object so that we can iterate over them without getting the dictionary class' methods.

//create the dictionary
var test = new dictionary();
//add 4 key values
test.add('Key1','Value1','Key2','Value2','Key3','Value3','Key4','Value4');

If you add an item with the same key again, the old value is overwritten with the new value. We can get individual items by treating the items objects properties as an array:

var Key2Value = test.items['Key2'];

We can also iterate over the items object like so:

for(property in test.items)
{
	console.log(property + '=' + test.items[property]);
}

getIds

I often need to pass ids from the backend of my cms to the front end so that jQuery can pick up the relevant ids to pass back through ajax. I'm not getting into the debate about class vs. rel for storing ids so the getIds function takes an optional parameter to specify which attribute the get the string to parse.

The function looks for strings that end in id_# and parses anything after the last _ as the value

Click on a string to see the results...

I have a class full of ids, click me (id_3 mp_id_4 fishy_id_7)
I have a rel full of ids, click me (id_19 mp_id_33)
I have a custom attribute full of ids, click me (id_47 upr_id_93)

Leave a Reply

Your email address will not be published. Required fields are marked *