Wednesday, March 28, 2012

Javascript Hash Table

HashTable in Javascript.
Using Javascript HashTables. 
JavaScript HashTables.

Didn't write here for a while now, and I just had a question from one of the programmers in our company.

Hash tables in .Net are piece of cake.  Simply creating a dictionary or an actual hashtable object and that it.
Well, as I always say - JavaScript is an Object Oriented scripting language. Because of that - hash tables in JavaScript are a piece of cake.  With cherries on top.

So, were gonna create an array which will contain the workers data in Json format (no need to panic, this is very simple example).
The Worker object will have Id.  Unfortunately, the array has some duplications and besides - all I need is the ID and the Name.

var hashtable = new Object();
var _array = [];
_array = FillWorkersData(); // demo function which will return json data.

for (var i in _array)
{
     var Worker = _array[i];
     if (_hashtable.hasOwnProperty(Worker.Id))
     {
          // hashtable already contains this worker
          continue;
     }

     _hashtable[Worker.Id] = Worker.Name;
}


Elad Shalom,
CTO at ITweetLive.com

10 comments:


  1. Thanks this made for intresting reading. I love your wordpress theme, i frequently come back here and i dont know why. I just seriously like your web site lol…

    ReplyDelete
  2. Why have you deleted my comment? It’s in fact beneficial unlike most of the comments posted here… I am going to post it again please do not get rid of it as lots of people will discover it very beneficial.

    ReplyDelete
  3. I know this is really boring and you are skipping to the next comment, but I just wanted to throw you a big thanks you cleared up some things for me!

    ReplyDelete