Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Possible Duplicate:
Loop through JavaScript object
Get array of object’s keys

Is there a way to use hashmaps in javascript. I found this page which shows one way of having hashmaps in javascript. Based on that I am storing the data as below:

var map = new Object();
map[myKey1] = myObj1;
map[myKey2] = myObj2;

function get(k) {
   return map[k];
}

But I want the keySet (all the keys) of the map object just like it is done in Java (map.keySet();).

Can anyone show me how can get all the keys present in this object?

share|improve this question

marked as duplicate by NimChimpsky, Sirko, Felix Kling, j08691, jsalonen Oct 16 '12 at 16:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 4 down vote accepted
for (var key in map) {
  if (map.hasOwnProperty(key)) {
    alert(key + " -> " + map[key]);
  }
}

http://stackoverflow.com/a/684692/106261

actually this way is much better :

var keys = Object.keys(map);
share|improve this answer

You can use for..in statement:

for (var key  in map) {
    return map[key];
}
share|improve this answer
2  
This will just return the first value and not the keyset. – Sirko Oct 16 '12 at 9:50
for(var propertyName in map) {
    // ...
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.