Tabela Hash w JavaScript

Używam tabeli hash w JavaScript i chcę pokazać następujące wartości w tabeli hash

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

Znalazłem następujący kod. Działa dla następujących danych.

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

Jak przypisać wartości one-[1,2] do tabeli hash i jak uzyskać do niej dostęp?

<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>
Jak to zrobić?
Author: Peter Mortensen, 2009-01-19

4 answers

Używając powyższej funkcji, wykonałbyś:

var myHash = new Hash('one',[1,10,5],'two', [2], 'three',[3,30,300]);

Oczywiście działa również:

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

Ponieważ wszystkie obiekty w JavaScript są tabel hash! Byłoby to jednak trudniejsze do powtórzenia, ponieważ użycie foreach(var item in object) również zapewniłoby Ci wszystkie jego funkcje, itp. ale to może wystarczyć w zależności od twoich potrzeb.

 72
Author: roryf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-20 05:35:03

Jeśli chcesz tylko zapisać pewne wartości statyczne w tabeli wyszukiwania, możesz użyć obiekt Literal (ten sam format używany przez JSON ), Aby zrobić to zwięźle:

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

A następnie uzyskaj do nich dostęp używając składni asocjacyjnej tablicy JavaScript:

alert(table['one']);    // Will alert with [1,10,5]
alert(table['one'][1]); // Will alert with 10
 32
Author: Shalom Craimer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-20 14:09:28

Możesz użyć mojej implementacji tabeli hashów JavaScript, jshashtable. Pozwala na użycie dowolnego obiektu jako klucza, a nie tylko łańcuchów.

 8
Author: Tim Down,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-08 22:05:59

Interpreter Javascript natywnie przechowuje obiekty w tabeli hash. Jeśli martwisz się o zanieczyszczenie łańcucha prototypów, zawsze możesz zrobić coś takiego:

// 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
 2
Author: anarchocurious,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-25 19:48:40