HashMap objects in javascript

Possible duplicate:
Loop through a JavaScript object
Get an array of object keys

Is there any way to use hashmaps in javascript. I found this page that shows one way to have hashmaps in javascript. Based on this, I save the data as shown below:

var map = new Object(); map[myKey1] = myObj1; map[myKey2] = myObj2; function get(k) { return map[k]; } 

But I want the keySet (all keys) of the map object in the same way as it is done in Java ( map.keySet(); ).

Can someone show me how to get all the keys present in this object?

+7
source share
3 answers
 for (var key in map) { if (map.hasOwnProperty(key)) { alert(key + " -> " + map[key]); } } 

stack overflow

actually this way is much better:

 var keys = Object.keys(map); 
+14
source

You can use the for..in :

 for (var key in map) { return map[key]; } 
-one
source
 for(var propertyName in map) { // ... } 
-one
source

All Articles