Como fazer array associativo / hashing em JavaScript

preciso de armazenar algumas estatísticas usando JavaScript de uma forma como eu faria em C#:

Dictionary<string, int> statistics;

statistics["Foo"] = 10;
statistics["Goo"] = statistics["Goo"] + 1;
statistics.Add("Zoo", 1);
Há algum Hashtable ou algo parecido com Dictionary<TKey, TValue> em JavaScript?
Como poderia armazenar valores dessa forma?

Author: Davide Cannizzo, 2009-07-30

11 answers

Use Os objectos JavaScript como arrays associativos.

Array associativo: em palavras simples arrays associativos usam Strings em vez de números inteiros como índice.

Criar um objecto com

var dictionary = {};

O JavaScript permite-lhe adicionar propriedades aos objectos usando a seguinte sintaxe:

Object.yourProperty = value;

Uma sintaxe alternativa para o mesmo é:

Object["yourProperty"] = value;

Se puder, crie também mapas de objectos chave-a-valor com o seguinte sintaxe:

var point = { x:3, y:2 };

point["x"] // returns 3
point.y // returns 2

Você pode iterar através de uma matriz associativa usando o for..in construir o ciclo como se segue

for(var key in Object.keys(dict)){
  var value = dict[key];
  /* use key/value for intended purpose */
}
 575
Author: Alek Davis, 2020-07-11 06:24:11
var associativeArray = {};
associativeArray["one"] = "First";
associativeArray["two"] = "Second";
associativeArray["three"] = "Third";

Se vem de uma língua orientada a objectos, deve verificar Este artigo.

 434
Author: Dani Cricco, 2015-09-23 12:39:46

Todos os navegadores modernos suportam um objecto JavaScript Map. Existem algumas razões que tornam o uso de um mapa melhor do que Objeto:

  • Um objecto tem um protótipo, por isso existem chaves predefinidas no mapa.
  • as chaves de um objecto são cadeias de caracteres, onde podem ser qualquer valor para um mapa.
  • Você pode obter o tamanho de um mapa facilmente, enquanto você tem que manter o controle do tamanho de um objeto.

Exemplo:

var myMap = new Map();

var keyObj = {},
    keyFunc = function () {},
    keyString = "a string";

myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, "value associated with keyObj");
myMap.set(keyFunc, "value associated with keyFunc");

myMap.size; // 3

myMap.get(keyString);    // "value associated with 'a string'"
myMap.get(keyObj);       // "value associated with keyObj"
myMap.get(keyFunc);      // "value associated with keyFunc"

Se você quiser chaves que não são referenciado a partir de outros objetos para ser lixo coletado, considere usar um WeakMap em vez de um mapa.

 164
Author: Vitalii Fedorenko, 2020-07-12 01:33:22
A menos que tenha uma razão específica para não o fazer, use apenas um objecto normal. As propriedades do objecto no JavaScript podem ser referenciadas usando a sintaxe do estilo hashtable:
var hashtable = {};
hashtable.foo = "bar";
hashtable['bar'] = "foo";

Ambos OS Elementos foo e bar podem agora ser referenciados como:

hashtable['foo'];
hashtable['bar'];

// Or
hashtable.foo;
hashtable.bar;
Claro que isto significa que as tuas chaves têm de ser cordas. Se eles não são cordas eles são convertidos internamente para cordas, então ainda pode funcionar. A sua quilometragem pode variar.
 133
Author: roryf, 2020-07-11 06:26:56

Uma vez que cada objeto em JavaScript se comporta como - e é geralmente implementado como - uma hashtable, eu simplesmente vou com isso...

var hashSweetHashTable = {};
 49
Author: Shog9, 2020-07-11 06:25:04

Em C# o código parece:

Dictionary<string,int> dictionary = new Dictionary<string,int>();
dictionary.add("sample1", 1);
dictionary.add("sample2", 2);

Ou

var dictionary = new Dictionary<string, int> {
    {"sample1", 1},
    {"sample2", 2}
};

Em JavaScript:

var dictionary = {
    "sample1": 1,
    "sample2": 2
}

Um objecto de dicionário C# contém métodos úteis, como dictionary.ContainsKey()

Em JavaScript, podíamos usar o hasOwnProperty Tipo:

if (dictionary.hasOwnProperty("sample1"))
    console.log("sample1 key found and its value is"+ dictionary["sample1"]);
 22
Author: Raj, 2020-07-12 01:34:20

Se necessita que as suas chaves sejam um objecto em vez de apenas cadeias de caracteres, então pode usar a minha jshashtable.

 18
Author: Tim Down, 2020-07-11 06:28:01

Anos atrás, eu implementei a seguinte hashtable, que tem tido algumas características que têm faltado para a classe Mapa. No entanto, já não é esse o caso. Agora é possível iterar sobre as entradas de um Mapa, obter uma matriz de suas chaves ou valores (ou ambas essas operações são implementadas copiar para um recém-alocado matriz, embora - isso é um desperdício de memória e a sua complexidade de tempo sempre vai ser tão ruim quanto O(n)), remover itens específicos, dada a sua chave, e limpar todo o mapa. Portanto, minha implementação hashtable só é útil para fins de compatibilidade, embora neste caso seria mais apropriado escrever um polifill adequado. Eu sugeria a qualquer um que usasse minha implementação hashtable para mudá-la de modo a torná-la um polifill para a classe Mapa.

function Hashtable() {
    this._map = new Map();
    this._indexes = new Map();
    this._keys = [];
    this._values = [];
    this.put = function(key, value) {
        var newKey = !this.containsKey(key);
        this._map.set(key, value);
        if (newKey) {
            this._indexes.set(key, this.length);
            this._keys.push(key);
            this._values.push(value);
        }
    };
    this.remove = function(key) {
        if (!this.containsKey(key))
            return;
        this._map.delete(key);
        var index = this._indexes.get(key);
        this._indexes.delete(key);
        this._keys.splice(index, 1);
        this._values.splice(index, 1);
    };
    this.indexOfKey = function(key) {
        return this._indexes.get(key);
    };
    this.indexOfValue = function(value) {
        return this._values.indexOf(value) != -1;
    };
    this.get = function(key) {
        return this._map.get(key);
    };
    this.entryAt = function(index) {
        var item = {};
        Object.defineProperty(item, "key", {
            value: this.keys[index],
            writable: false
        });
        Object.defineProperty(item, "value", {
            value: this.values[index],
            writable: false
        });
        return item;
    };
    this.clear = function() {
        var length = this.length;
        for (var i = 0; i < length; i++) {
            var key = this.keys[i];
            this._map.delete(key);
            this._indexes.delete(key);
        }
        this._keys.splice(0, length);
    };
    this.containsKey = function(key) {
        return this._map.has(key);
    };
    this.containsValue = function(value) {
        return this._values.indexOf(value) != -1;
    };
    this.forEach = function(iterator) {
        for (var i = 0; i < this.length; i++)
            iterator(this.keys[i], this.values[i], i);
    };
    Object.defineProperty(this, "length", {
        get: function() {
            return this._keys.length;
        }
    });
    Object.defineProperty(this, "keys", {
        get: function() {
            return this._keys;
        }
    });
    Object.defineProperty(this, "values", {
        get: function() {
            return this._values;
        }
    });
    Object.defineProperty(this, "entries", {
        get: function() {
            var entries = new Array(this.length);
            for (var i = 0; i < entries.length; i++)
                entries[i] = this.entryAt(i);
            return entries;
        }
    });
}

Documentação da classe Hashtable

Métodos:

  • get(key)
    Devolve o valor associado à chave indicada.
    Parâmetros:
    key: A chave para recuperar o valor.

  • put(key, value)
    Associa o valor especificado à chave especificada.
    parâmetros:
    key: a chave para a qual se associa o valor.
    value: o valor a associar à chave.

  • remove(key)
    Remove a chave indicada, em conjunto com o valor a ela associado.
    Parâmetros:
    key: A chave para remover.

  • clear()
    Limpa a hashtable inteira, removendo todas as entradas.

  • indexOfKey(key)
    Retorna o índice da chave especificada, de acordo com os itens de ordem foram adicionados.
    Parâmetros:
    key: A chave para obter o índice.

  • indexOfValue(value)
    Retorna o índice do valor especificado, de acordo com os itens de ordem foram adicionados.
    parâmetros:
    value: o valor para obter o índice.
    observações:
    Esta informação é obtida através da indexOf() método de uma matriz, então os objetos são comparados pela identidade.

  • entryAt(index)
    Devolve um objecto com propriedades key e value, representando o item no índice indicado.
    Parâmetros:
    index: O índice da entrada para chegar.

  • containsKey(key)
    Devolve se a hashtable contém a chave indicada.
    parâmetros: A chave para procurar.

  • containsValue(value)
    Devolve se o a hashtable contém o valor indicado.
    parâmetros:
    value: o valor a procurar.

  • forEach(iterator)
    Itera através de todas as entradas na hashtable, chamando especificado iterator.
    Parâmetros:
    iterator: Um método com três parâmetros, key, value e index, onde index representa o índice da entrada de acordo com a ordem em que foi adicionado.

Propriedades:

  • length Apenas para leitura)
    Obtém a contagem das entradas no hashtable.

  • keys (apenas para leitura)
    Obtém um conjunto de todas as chaves do hashtable.

  • values (apenas para leitura)
    Obtém um array de todos os valores na hashtable.

  • entries (apenas para leitura)
    Obtém uma lista de todas as entradas no hashtable. Eles são representados do mesmo modo que o método entryAt() o.

 8
Author: Davide Cannizzo, 2020-11-20 13:16:10
function HashTable() {
    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_previous;
        if (typeof (this.items[in_key]) != 'undefined') {
            this.length--;
            var tmp_previous = this.items[in_key];
            delete this.items[in_key];
        }

        return tmp_previous;
    }

    this.getItem = function (in_key) {
        return this.items[in_key];
    }

    this.setItem = function (in_key, in_value) {
        var tmp_previous;
        if (typeof (in_value) != 'undefined') {
            if (typeof (this.items[in_key]) == 'undefined') {
                this.length++;
            } else {
                tmp_previous = this.items[in_key];
            }

            this.items[in_key] = in_value;
        }

        return tmp_previous;
    }

    this.hasItem = function (in_key) {
        return typeof (this.items[in_key]) != 'undefined';
    }

    this.clear = function () {
        for (var i in this.items) {
            delete this.items[i];
        }

        this.length = 0;
    }
}
 6
Author: Birey, 2011-11-04 15:51:21

Https://gist.github.com/alexhawkins/f6329420f40e5cafa0a4

var HashTable = function() {
  this._storage = [];
  this._count = 0;
  this._limit = 8;
}


HashTable.prototype.insert = function(key, value) {

  // Create an index for our storage location by passing
  // it through our hashing function
  var index = this.hashFunc(key, this._limit);

  // Retrieve the bucket at this particular index in
  // our storage, if one exists
  //[[ [k,v], [k,v], [k,v] ] , [ [k,v], [k,v] ]  [ [k,v] ] ]
  var bucket = this._storage[index]

  // Does a bucket exist or do we get undefined
  // when trying to retrieve said index?
  if (!bucket) {
    // Create the bucket
    var bucket = [];
    // Insert the bucket into our hashTable
    this._storage[index] = bucket;
  }

  var override = false;

  // Now iterate through our bucket to see if there are any conflicting
  // key value pairs within our bucket. If there are any, override them.
  for (var i = 0; i < bucket.length; i++) {
    var tuple = bucket[i];
    if (tuple[0] === key) {

      // Override value stored at this key
      tuple[1] = value;
      override = true;
    }
  }

  if (!override) {
    // Create a new tuple in our bucket.
    // Note that this could either be the new empty bucket we created above
    // or a bucket with other tupules with keys that are different than
    // the key of the tuple we are inserting. These tupules are in the same
    // bucket because their keys all equate to the same numeric index when
    // passing through our hash function.
    bucket.push([key, value]);
    this._count++

    // Now that we've added our new key/val pair to our storage
    // let's check to see if we need to resize our storage
    if (this._count > this._limit * 0.75) {
      this.resize(this._limit * 2);
    }
  }
  return this;
};


HashTable.prototype.remove = function(key) {
  var index = this.hashFunc(key, this._limit);
  var bucket = this._storage[index];
  if (!bucket) {
    return null;
  }

  // Iterate over the bucket
  for (var i = 0; i < bucket.length; i++) {
    var tuple = bucket[i];

    // Check to see if key is inside bucket
    if (tuple[0] === key) {

      // If it is, get rid of this tuple
      bucket.splice(i, 1);
      this._count--;
      if (this._count < this._limit * 0.25) {
        this._resize(this._limit / 2);
      }
      return tuple[1];
    }
  }
};


HashTable.prototype.retrieve = function(key) {
  var index = this.hashFunc(key, this._limit);
  var bucket = this._storage[index];

  if (!bucket) {
    return null;
  }

  for (var i = 0; i < bucket.length; i++) {
    var tuple = bucket[i];
    if (tuple[0] === key) {
      return tuple[1];
    }
  }

  return null;
};


HashTable.prototype.hashFunc = function(str, max) {
  var hash = 0;
  for (var i = 0; i < str.length; i++) {
    var letter = str[i];
    hash = (hash << 5) + letter.charCodeAt(0);
    hash = (hash & hash) % max;
  }
  return hash;
};


HashTable.prototype.resize = function(newLimit) {
  var oldStorage = this._storage;

  this._limit = newLimit;
  this._count = 0;
  this._storage = [];

  oldStorage.forEach(function(bucket) {
    if (!bucket) {
      return;
    }
    for (var i = 0; i < bucket.length; i++) {
      var tuple = bucket[i];
      this.insert(tuple[0], tuple[1]);
    }
  }.bind(this));
};


HashTable.prototype.retrieveAll = function() {
  console.log(this._storage);
  //console.log(this._limit);
};

/******************************TESTS*******************************/

var hashT = new HashTable();

hashT.insert('Alex Hawkins', '510-599-1930');
//hashT.retrieve();
//[ , , , [ [ 'Alex Hawkins', '510-599-1930' ] ] ]
hashT.insert('Boo Radley', '520-589-1970');
//hashT.retrieve();
//[ , [ [ 'Boo Radley', '520-589-1970' ] ], , [ [ 'Alex Hawkins', '510-599-1930' ] ] ]
hashT.insert('Vance Carter', '120-589-1970').insert('Rick Mires', '520-589-1970').insert('Tom Bradey', '520-589-1970').insert('Biff Tanin', '520-589-1970');
//hashT.retrieveAll();
/*
[ ,
  [ [ 'Boo Radley', '520-589-1970' ],
    [ 'Tom Bradey', '520-589-1970' ] ],
  ,
  [ [ 'Alex Hawkins', '510-599-1930' ],
    [ 'Rick Mires', '520-589-1970' ] ],
  ,
  ,
  [ [ 'Biff Tanin', '520-589-1970' ] ] ]
*/

// Override example (Phone Number Change)
//
hashT.insert('Rick Mires', '650-589-1970').insert('Tom Bradey', '818-589-1970').insert('Biff Tanin', '987-589-1970');
//hashT.retrieveAll();

/*
[ ,
  [ [ 'Boo Radley', '520-589-1970' ],
    [ 'Tom Bradey', '818-589-1970' ] ],
  ,
  [ [ 'Alex Hawkins', '510-599-1930' ],
    [ 'Rick Mires', '650-589-1970' ] ],
  ,
  ,
  [ [ 'Biff Tanin', '987-589-1970' ] ] ]

*/

hashT.remove('Rick Mires');
hashT.remove('Tom Bradey');
//hashT.retrieveAll();

/*
[ ,
  [ [ 'Boo Radley', '520-589-1970' ] ],
  ,
  [ [ 'Alex Hawkins', '510-599-1930' ] ],
  ,
  ,
  [ [ 'Biff Tanin', '987-589-1970' ] ] ]


*/

hashT.insert('Dick Mires', '650-589-1970').insert('Lam James', '818-589-1970').insert('Ricky Ticky Tavi', '987-589-1970');
hashT.retrieveAll();


/* NOTICE HOW THE HASH TABLE HAS NOW DOUBLED IN SIZE UPON REACHING 75% CAPACITY, i.e. 6/8. It is now size 16.
 [,
  ,
  [ [ 'Vance Carter', '120-589-1970' ] ],
  [ [ 'Alex Hawkins', '510-599-1930' ],
    [ 'Dick Mires', '650-589-1970' ],
    [ 'Lam James', '818-589-1970' ] ],
  ,
  ,
  ,
  ,
  ,
  [ [ 'Boo Radley', '520-589-1970' ],
    [ 'Ricky Ticky Tavi', '987-589-1970' ] ],
  ,
  ,
  ,
  ,
  [ [ 'Biff Tanin', '987-589-1970' ] ] ]

*/

console.log(hashT.retrieve('Lam James'));  // 818-589-1970
console.log(hashT.retrieve('Dick Mires')); // 650-589-1970
console.log(hashT.retrieve('Ricky Ticky Tavi')); //987-589-1970
console.log(hashT.retrieve('Alex Hawkins')); // 510-599-1930
console.log(hashT.retrieve('Lebron James')); // null
 2
Author: Alex Hawkins, 2020-07-11 06:32:25

Você pode criar um usando o seguinte:

var dictionary = { Name:"Some Programmer", Age:24, Job:"Writing Programs"  };

// Iterate over using keys
for (var key in dictionary) {
  console.log("Key: " + key + " , " + "Value: "+ dictionary[key]);
}

// Access a key using object notation:
console.log("Her name is: " + dictionary.Name)
 1
Author: Ali Ezzat Odeh, 2020-07-12 23:10:33