49

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
venkatachalam
  • 102,353
  • 31
  • 72
  • 77

4 Answers4

88

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
roryf
  • 29,592
  • 16
  • 81
  • 103
  • 26
    keys = Object.keys(myHash) will give one an array of the keys, so in this case it would return ['one','two','three']. You can then iterate on them using for(var i=0; i – zupa Jan 16 '13 at 15:17
  • 1
    I don't find Hash in javascript. (Hash is not defined.) Can you provide the link – jforjs Oct 18 '16 at 08:36
  • 1
    @jforjs he is referring to the Hash function declared in the question. "Using the function above..." – Estevan Maito Apr 07 '17 at 16:35
35

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
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Shalom Craimer
  • 20,659
  • 8
  • 70
  • 106
8

You could use my JavaScript hash table implementation, jshashtable. It allows any object to be used as a key, not just strings.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
5

The Javascript interpreter natively stores objects in a hash table. If you're worried about contamination from the prototype chain, you can always do something like this:

// Simple ECMA5 hash table
Hash = function(oSource){
  for(sKey in oSource) if(Object.prototype.hasOwnProperty.call(oSource, sKey)) this[sKey] = oSource[sKey];
};
Hash.prototype = Object.create(null);

var oHash = new Hash({foo: 'bar'});
oHash.foo === 'bar'; // true
oHash['foo'] === 'bar'; // true
oHash['meow'] = 'another prop'; // true
oHash.hasOwnProperty === undefined; // true
Object.keys(oHash); // ['foo', 'meow']
oHash instanceof Hash; // true
anarchocurious
  • 590
  • 6
  • 13