Friday 9 September 2011

Hash Table or Key/Value pair Array in JavaScript

Key/Value or hash table sometimes is very handy and helpful on implementing your business logic. In .Net world, I use a lot of Dictionary<TKey, TValue> collection object. How to do the same thing using Javascript is bit a tricky to me as I spent most of my time in the past using Microsoft C++ and C# developing server-side system. Google it a bit and here is my working solution including of declaration, insertion and deletion of items.
 
// declare a hash table
var myTable = new Array();
// add new items to the table
myTable['key1'] = 'hello';
myTable['key2'] = 'world';
myTable['key3'] = ' world.';

// remove a item from the table
delete myTable['key2'];
 

No comments:

Post a Comment