【问题标题】:Hash table in JavaScriptJavaScript 中的哈希表
【发布时间】:2010-10-02 04:46:01
【问题描述】:

我在 JavaScript 中使用哈希表,我想在哈希表中显示以下值

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

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

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

如何将 one-[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>

我该怎么做?

【问题讨论】:

    标签: javascript hashtable


    【解决方案1】:

    使用上面的函数,你会这样做:

    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) 也可以获得它的所有功能等,但这可能就足够了,这取决于你的需要。

    【讨论】:

    • keys = Object.keys(myHash) 会给一个键数组,所以在这种情况下它会返回 ['one','two','three']。然后,您可以使用 for(var i=0; i 对它们进行迭代
    • 我在 javascript 中找不到哈希。 (哈希未定义。)你能提供链接吗
    • @jforjs 他指的是问题中声明的哈希函数。 “使用上面的函数……”
    【解决方案2】:

    如果您只想在查找表中存储一些静态值,您可以使用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
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        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
        

        【讨论】:

          猜你喜欢
          • 2016-08-04
          • 1970-01-01
          • 1970-01-01
          • 2012-03-09
          • 1970-01-01
          • 1970-01-01
          • 2014-03-03
          • 2012-08-23
          • 2013-09-10
          相关资源
          最近更新 更多