I am using a hash table in JavaScript, and I want to show the values of the following in a hash table
one -[1,10,5] two -[2] three -[3, 30, 300, etc.]
I have found the following code. It works for the following data.
one -[1] two -[2] three-[3]
How do I assign one-[1,2] values to a hash table and how do I access it?
<script type="text/javascript"> function Hash() { this.length = 0; this.items = new Array(); for (var i = 0; i < arguments.length; i += 2) { if (typeof(arguments[i + 1]) != 'undefined') { this.items[arguments[i]] = arguments[i + 1]; this.length++; } } this.removeItem = function(in_key) { var tmp_value; if (typeof(this.items[in_key]) != 'undefined') { this.length--; var tmp_value = this.items[in_key]; delete this.items[in_key]; } return tmp_value; } this.getItem = function(in_key) { return this.items[in_key]; } this.setItem = function(in_key, in_value) { if (typeof(in_value) != 'undefined') { if (typeof(this.items[in_key]) == 'undefined') { this.length++; } this.items[in_key] = in_value; } return in_value; } this.hasItem = function(in_key) { return typeof(this.items[in_key]) != 'undefined'; } } var myHash = new Hash('one',1,'two', 2, 'three',3 ); for (var i in myHash.items) { alert('key is: ' + i + ', value is: ' + myHash.items[i]); } </script>
How do I do it?
Hash Tables are a data structure that allow you to create a list of paired values. You can then retrieve a certain value by using the key for that value, which you put into the table beforehand.
A JavaScript Object is an example of a Hash Table because data is represented a key/value pairs. A hashing function can be used to map the key to an index by taking an input of any size and returning a hash code identifier of a fixed size.
The hash function (hash algorithm) takes a key (string) and transforms it into a number. It then remaps that number into an index in an array. Different words are mapped to different numbers by a hash function. A hash function is irreversible. It is a one-way algorithm.
There exist two components of Hash tables in JavaScript: an “Object” and a “Hash Function”: Object: An object contains the hash table in which the data is stored. It holds all the “key-value” pairs of the hash table.
Using the function above, you would do:
var myHash = new Hash('one',[1,10,5],'two', [2], 'three',[3,30,300]);
Of course, the following would also work:
var myHash = {}; // New object myHash['one'] = [1,10,5]; myHash['two'] = [2]; myHash['three'] = [3, 30, 300];
since all objects in JavaScript are hash tables! It would, however, be harder to iterate over since using foreach(var item in object)
would also get you all its functions, etc., but that might be enough depending on your needs.
If all you want to do is store some static values in a lookup table, you can use an Object Literal (the same format used by JSON) to do it compactly:
var table = { one: [1,10,5], two: [2], three: [3, 30, 300] }
And then access them using JavaScript's associative array syntax:
alert(table['one']); // Will alert with [1,10,5] alert(table['one'][1]); // Will alert with 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With