15

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?

2
  • And how is this Java? Commented Dec 16, 2014 at 18:13
  • 2
    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. Commented Dec 16, 2014 at 18:15

5 Answers 5

20

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

Sign up to request clarification or add additional context in comments.

1 Comment

17

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])
}

Comments

1

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

Comments

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.

1 Comment

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?
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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.