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:



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:


Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: