eu preciso fazer um dicionário em javascript como este
não me lembro da notação exata, mas era algo como:
states_dictionary={ CT=[alex,harry], AK=[liza,alex], TX=[fred, harry] ........ }
existe tal coisa em javascript?
eu preciso fazer um dicionário em javascript como este
não me lembro da notação exata, mas era algo como:
states_dictionary={ CT=[alex,harry], AK=[liza,alex], TX=[fred, harry] ........ }
existe tal coisa em javascript?
Respostas:
Esta é uma postagem antiga, mas achei que deveria fornecer uma resposta ilustrada de qualquer maneira.
Use a notação de objeto do javascript. Igual a:
states_dictionary={
"CT":["alex","harry"],
"AK":["liza","alex"],
"TX":["fred", "harry"]
};
E para acessar os valores:
states_dictionary.AK[0] //which is liza
ou você pode usar a notação de objeto literal javascript, em que as chaves não precisam estar entre aspas:
states_dictionary={
CT:["alex","harry"],
AK:["liza","alex"],
TX:["fred", "harry"]
};
Object.hasOwnProperty.call(dictionary, key)(caso contrário, o usuário pode inserir um valor de valueOf e dictionary['valueOf']retornar a Object.valueOf()função pertencente ao protótipo do objeto que provavelmente não é o que seu código esperaria - bug potencial ou problema de segurança ) Se a chave não for um tipo de string, é necessário tomar cuidado, caso contrário, as conversões numérica e toString implícitas causarão problemas. O Maptipo ES6 foi projetado para fornecer funcionalidade estendida para dicionários.
Não havia matrizes associativas reais em Javascript até 2015 (lançamento do ECMAScript 6). Desde então, você pode usar o objeto Map como estados Robocat. Procure os detalhes no MDN . Exemplo:
let map = new Map();
map.set('key', {'value1', 'value2'});
let values = map.get('key');
Sem suporte para ES6, você pode tentar usar objetos:
var x = new Object();
x["Key"] = "Value";
No entanto, com objetos não é possível usar propriedades ou métodos de array típicos como array.length. Pelo menos é possível acessar o "objeto-array" em um for-in-loop.
Sei que essa é uma pergunta antiga, mas ela aparece no Google quando você pesquisa por 'dicionários de javascript', então gostaria de acrescentar às respostas acima que no ECMAScript 6, o Mapobjeto oficial foi introduzido, que é um dicionário implementação:
var dict = new Map();
dict.set("foo", "bar");
//returns "bar"
dict.get("foo");
Ao contrário dos objetos normais do javascript, ele permite qualquer objeto como uma chave:
var foo = {};
var bar = {};
var dict = new Map();
dict.set(foo, "Foo");
dict.set(bar, "Bar");
//returns "Bar"
dict.get(bar);
//returns "Foo"
dict.get(foo);
//returns undefined, as {} !== foo and {} !== bar
dict.get({});
dict = { key: value)?
Criei um dicionário simples em JS aqui:
function JSdict() {
this.Keys = [];
this.Values = [];
}
// Check if dictionary extensions aren't implemented yet.
// Returns value of a key
if (!JSdict.prototype.getVal) {
JSdict.prototype.getVal = function (key) {
if (key == null) {
return "Key cannot be null";
}
for (var i = 0; i < this.Keys.length; i++) {
if (this.Keys[i] == key) {
return this.Values[i];
}
}
return "Key not found!";
}
}
// Check if dictionary extensions aren't implemented yet.
// Updates value of a key
if (!JSdict.prototype.update) {
JSdict.prototype.update = function (key, val) {
if (key == null || val == null) {
return "Key or Value cannot be null";
}
// Verify dict integrity before each operation
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
var flag = false;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
this.Values[i] = val;
flag = true;
break;
}
}
if (!flag) {
return "Key does not exist";
}
}
}
// Check if dictionary extensions aren't implemented yet.
// Adds a unique key value pair
if (!JSdict.prototype.add) {
JSdict.prototype.add = function (key, val) {
// Allow only strings or numbers as keys
if (typeof (key) == "number" || typeof (key) == "string") {
if (key == null || val == null) {
return "Key or Value cannot be null";
}
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
return "Duplicate keys not allowed!";
}
}
this.Keys.push(key);
this.Values.push(val);
}
else {
return "Only number or string can be key!";
}
}
}
// Check if dictionary extensions aren't implemented yet.
// Removes a key value pair
if (!JSdict.prototype.remove) {
JSdict.prototype.remove = function (key) {
if (key == null) {
return "Key cannot be null";
}
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
var flag = false;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
this.Keys.shift(key);
this.Values.shift(this.Values[i]);
flag = true;
break;
}
}
if (!flag) {
return "Key does not exist";
}
}
}
var dict = new JSdict();
dict.add(1, "one")
dict.add(1, "one more")
"Duplicate keys not allowed!"
dict.getVal(1)
"one"
dict.update(1, "onne")
dict.getVal(1)
"onne"
dict.remove(1)
dict.getVal(1)
"Key not found!"
Esta é apenas uma simulação básica. Ele pode ser otimizado ainda mais através da implementação de um algoritmo de tempo de execução melhor para trabalhar em complexidade de tempo pelo menos O (nlogn) ou até menos. Como mesclar / classificação rápida em arrays e, em seguida, algumas pesquisas B para pesquisas. Não tentei ou pesquisei sobre o mapeamento de uma função hash em JS.
Além disso, a chave e o valor para o objeto JSdict podem ser transformados em variáveis privadas para serem sorrateiros.
Espero que isto ajude!
EDITAR >> Depois de implementar o acima, eu pessoalmente usei os objetos JS como arrays associativos que estão disponíveis prontos para uso.
No entanto , gostaria de fazer uma menção especial sobre dois métodos que realmente se mostraram úteis para torná-lo uma experiência hashtable conveniente.
Viz: dict.hasOwnProperty (key) e delete dict [key]
Leia esta postagem como um bom recurso sobre essa implementação / uso. Criação dinâmica de chaves em array associativo JavaScript
Obrigado!
Uma pergunta antiga, mas recentemente precisei fazer uma porta AS3> JS e, por uma questão de velocidade, escrevi um objeto Dicionário simples no estilo AS3 para JS:
http://jsfiddle.net/MickMalone1983/VEpFf/2/
Se você não sabia, o dicionário AS3 permite que você use qualquer objeto como chave, ao invés de apenas strings. Eles são muito úteis assim que você encontrar um uso para eles.
Não é tão rápido quanto um objeto nativo seria, mas não encontrei nenhum problema significativo com ele a esse respeito.
API:
//Constructor
var dict = new Dict(overwrite:Boolean);
//If overwrite, allows over-writing of duplicate keys,
//otherwise, will not add duplicate keys to dictionary.
dict.put(key, value);//Add a pair
dict.get(key);//Get value from key
dict.remove(key);//Remove pair by key
dict.clearAll(value);//Remove all pairs with this value
dict.iterate(function(key, value){//Send all pairs as arguments to this function:
console.log(key+' is key for '+value);
});
dict.get(key);//Get value from key
O Firefox 13+ fornece uma implementação experimental do mapobjeto semelhante ao dictobjeto em python. Especificações aqui .
Só está disponível no firefox, mas parece melhor do que usar atributos de a new Object(). Citação da documentação:
- Um objeto tem um protótipo, portanto, há chaves padrão no mapa. No entanto, isso pode ser contornado usando
map = Object.create(null).- As chaves de um
ObjectsãoStrings, onde podem ter qualquer valor para aMap.- Você pode obter o tamanho de um
Mapfacilmente enquanto precisa controlar manualmente o tamanho de umObject.