JavaScript中的哈希表

50

我在JavaScript中使用了哈希表,我想展示以下哈希表的值

one   -[1,10,5]
two   -[2]
three -[3, 30, 300, etc.]

我找到了以下代码。它适用于以下数据。

   one  -[1]
   two  -[2]
   three-[3]

如何将一个一维数组 [1,2] 赋值给哈希表,并且如何访问它?

<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>

我该怎么做?

4个回答

88
使用上述函数,您将执行以下操作:
var myHash = new Hash('one',[1,10,5],'two', [2], 'three',[3,30,300]);

当然,以下方法也可以起到同样的效果:

var myHash = {}; // New object
myHash['one'] = [1,10,5];
myHash['two'] = [2];
myHash['three'] = [3, 30, 300];

因为在JavaScript中,所有的对象都是哈希表!然而,如果使用foreach(var item in object)进行迭代,也会获取到它所有的函数等内容,这可能会让迭代变得更加困难。但根据你的需求,这可能已经足够了。


26
keys = Object.keys(myHash)会返回一个键的数组,因此在这种情况下它将返回['one', 'two', 'three']。然后,您可以使用for(var i=0; i<keys.length; i++) {}来对它们进行迭代。 - zupa
1
我在JavaScript中找不到哈希(Hash未定义)。你能提供链接吗? - jforjs
1
@jforjs他指的是问题中声明的哈希函数。 “使用上面的函数…” - Estevan Maito

35

如果您只想在查找表中存储一些静态值,您可以使用Object Literal(与JSON使用的相同格式)来紧凑地完成它:

var table = { one: [1,10,5], two: [2], three: [3, 30, 300] }

然后使用JavaScript的关联数组语法来访问它们:

alert(table['one']);    // Will alert with [1,10,5]
alert(table['one'][1]); // Will alert with 10

8
你可以使用我的JavaScript哈希表实现,jshashtable。它允许任何对象作为键,而不仅仅是字符串。

5

Javascript解释器在本地使用哈希表存储对象。如果您担心原型链的污染,您可以像这样做:

// 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接