Javascript 有没有办法在javascript中创建hashmap并像添加和删除值一样操作它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27511174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to create hashmap in javascript and manipulate it like adding and deleting values
提问by user1817439
My requirement is to store key-value pairs in a data structure and fetch or delete the pairs when necessary using keys in JavaScript.
我的要求是将键值对存储在数据结构中,并在必要时使用 JavaScript 中的键来获取或删除这些对。
How can I do it in JavaScript as one does it in Java?
我怎样才能像在 Java 中那样在 JavaScript 中做到这一点?
I have seen an answer creating an instance of hash map like:
我看过一个创建哈希映射实例的答案,例如:
var hash={};
Now Ie can add values in it like:
现在,即可以在其中添加值,例如:
hash={ "January":"1","Feb":"2" }
Can I insert values dynamically using keys and fetch them and also get the size of the hash map?
我可以使用键动态插入值并获取它们并获取哈希映射的大小吗?
回答by juvian
Yes, that's an associative array (var hash = new Object();)
是的,这是一个关联数组 ( var hash = new Object();)
//You can add in these ways:
hash.January='1';
hash['Feb']='2';
//For length:
console.log(Object.keys(hash).length)
//To fetch by key:
console.log(hash['Feb']) // '2'
//To fetch all:
for(var key in hash){
console.log('key is :' + key + ' and value is : '+ hash[key])
}
回答by user9269553
You can use the built-in Map object.
您可以使用内置的 Map 对象。
var myMap = new Map();
var keyObj = {};
myMap.set(keyObj, "value");
myMap.get(keyObj);
for (var [key, value] of myMap) {
console.log(key + " = " + value);
}
More information here : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Map
更多信息:https: //developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Map
回答by b4hand
hash["dynamic"] = 5will insert something. Object.keys(hash).lengthwill get the size.
hash["dynamic"] = 5会插入一些东西。Object.keys(hash).length将得到大小。
回答by Michael Garner
Please see this guide on JavaScript Objects: http://www.w3schools.com/js/js_objects.asp
请参阅有关 JavaScript 对象的本指南:http: //www.w3schools.com/js/js_objects.asp
JavaScript objects can be accessed in a number of ways. Let's take this object as an example:
可以通过多种方式访问 JavaScript 对象。我们以这个对象为例:
var person = {
first_name: "John",
last_name: "Smith",
age: 39
};
If you wished to access the first_name you could do:
如果你想访问 first_name 你可以这样做:
person.first_name;
Or
或者
person['first_name'];
These would both retrieve the value.
这些都将检索值。
You may also set the value in similar fashion:
你也可以用类似的方式设置值:
person.first_name = "Michael";
Now, if you were referring to creating an iterator using a keys() method like in Java then you can't do that exactly, but you can do something similar.
现在,如果您指的是像在 Java 中一样使用 keys() 方法创建迭代器,那么您不能完全做到这一点,但您可以做类似的事情。
You can iterate over the object however in a similar manner that you would iterate over an array:
您可以迭代对象,但是以与迭代数组类似的方式:
for (var property in object) {
if (object.hasOwnProperty(property)) {
// do stuff
}
}
}
A newer built-in is also available where you can use Object.keys(person) to get an array of the objects keys. Read more here:
还可以使用更新的内置函数,您可以在其中使用 Object.keys(person) 来获取对象键的数组。在此处阅读更多信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
I suggest using Google a little more. There are plenty of resources out there for this type of question. You would find the answer more quickly than someone would respond on here.
我建议多使用谷歌。有很多资源可以解决此类问题。您会比这里的其他人更快地找到答案。
回答by jacquel
elegant and simple javascript hashmap code
var hashMap=function(){
this.hashDict={};//dictionary
this.size=0;
this.debug=true;
return this;
}
now to insert :
现在插入:
hashMap.prototype.put=function(_key,_value){
if (!this.hashDict.hasOwnProperty(_key)) {
this.hashDict[_key] = _value;
++this.size;
}
else if(this.debug)
throw 'duplicate keys not allowed. key : '+_key;
}
you can also get the size using and perform all other manipulations
您还可以使用并执行所有其他操作来获取大小
only you have to do is create the object of class hash map like :
只有你需要做的是创建类哈希映射的对象,如:
hashmap n = new hashMap();
n.put('key','value');
n.get('key');
n.size; // gives size

