Hash Tables in JavaScript

One of my C# code snippets had a Hash Table functionality and I was trying to emulate the same in JavaScript. JavaScript does not have a built-in HashTable, however you can make use of object properties as hash-type tables.

Here’s an example:

javascript-hashtable

OUTPUT: Jane

The limitation in the above method is that you can use only string or integers as keys. When you are using integers as keys, it gets converted to string while setting the property. If you want to use objects as keys, check this nice library called jshashtable

You can also write the code shown above in the following way:

javascript-hashtable-example




About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: