11

My requirement is to store key-value pairs in a data structure and fetch or delete the pairs when necessary using keys in JavaScript.

How can I do it in JavaScript as one does it in Java?

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?

b4hand
  • 9,550
  • 4
  • 44
  • 49
user1817439
  • 123
  • 1
  • 1
  • 4
  • And how is this Java? – fge Dec 16 '14 at 18:13
  • 1
    What have you tried? You should be able to Google "javascript objects" or "javascript map" or "javascript associate array" and see all the information you need. – mkobit Dec 16 '14 at 18:15

5 Answers5

17

You can use the built-in Map object.

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

user9269553
  • 171
  • 1
  • 4
  • 4
    English link for whoever wants it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map – paydro Jul 10 '19 at 21:43
16

Yes, that's an associative array (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])
}
juvian
  • 15,875
  • 2
  • 37
  • 38
1

hash["dynamic"] = 5 will insert something. Object.keys(hash).length will get the size.

b4hand
  • 9,550
  • 4
  • 44
  • 49
1

Please see this guide on JavaScript Objects: 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:

var person = {
  first_name: "John",
  last_name: "Smith",
  age: 39
};

If you wished to access the first_name you could do:

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.

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:

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.

Michael Garner
  • 1,042
  • 1
  • 10
  • 19
  • 1
    No need to use `object.hasOwnProperty()` anymore if you create your map with `Object.create(null)`. This creates a prototype-less object so it contains no properties other than what you put into it. I think this is new with ECMAScript 5? – Andy Jan 27 '15 at 05:34
1

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 
jacquel
  • 946
  • 1
  • 7
  • 10