44

I'm using this method to make artificial 'hashmaps' in javascript. All I am aiming for is key|value pairs, the actual run time is not important. The method below works fine.

Are there any other ways to loop through this?

for (var i in a_hashMap[i]) {
    console.log('Key is: ' + i + '. Value is: ' + a_hashMap[i]);
} 

I run into a problem where this outputs a bunch of undefined keys after the first key, when the array only contains one entry. I have a feeling it is because the code is within a loop which uses i, even though when I follow in debug it shouldn't be happening. I also cannot change i as the for loop seems to not understand the replaced var at all.

Anyone any ideas?

myol
  • 7,355
  • 18
  • 75
  • 124
  • 1
    Just watch this thread: [Javascript Hashmap Equivalent][1] Hope it helps you. [1]: http://stackoverflow.com/questions/368280/javascript-hashmap-equivalent – elvenbyte Jul 19 '11 at 14:35

9 Answers9

61
for (var i in a_hashmap[i])

is not correct. It should be

for (var i in a_hashmap)

which means "loop over the properties of a_hashmap, assigning each property name in turn to i"

jhurshman
  • 5,643
  • 1
  • 24
  • 16
12
for (var i = 0, keys = Object.keys(a_hashmap), ii = keys.length; i < ii; i++) {
  console.log('key : ' + keys[i] + ' val : ' + a_hashmap[keys[i]]);
}
Raynos
  • 162,380
  • 56
  • 343
  • 392
7

You can use JQuery function

$.each( hashMap, function(index,value){
 console.log("Index = " + index + " value = " + value); 
})
GothamGirl
  • 269
  • 2
  • 13
dchhetri
  • 6,590
  • 4
  • 39
  • 55
6

Do you mean

for (var i in a_hashmap) { // Or `let` if you're a language pedant :-)
   ...
}

i is undefined when the for-loop gets set up.

spraff
  • 30,806
  • 22
  • 110
  • 214
  • Didn't think [to check](http://en.wikipedia.org/wiki/JavaScript#Versions). I suppose you're right. – spraff Jul 19 '11 at 14:34
  • Downvote? `let` aside, this is the same as the accepted answer :-/ – spraff Jul 19 '11 at 15:28
  • +1 because this does not deserve a -1. @spraff, you might want to add an update (edit the answer) stating what you have stated in comments. – Nivas Jul 19 '11 at 15:37
  • +1 for mentioning 'let'. This is best use case of let. Anyone down voting, probably ignored the fact, JS is used on servers as well. – SJ00 Dec 08 '17 at 11:47
5

Try this in order to print console correctly...

for(var i in a_hashMap) {
    if (a_hashMap.hasOwnProperty(i)) {
        console.log('Key is: ' + i + '. Value is: ' + a_hashMap[i]);
    }
}
Atmaram
  • 132
  • 2
  • 11
4

Iterating through a map in vanilla Javacsript is simple .

var map = {...};//your map defined here
for(var index in map)
 {
       var mapKey = index;//This is the map's key.
       for(i = 0 ; i < map[mapKey].length ; i++)
        {
              var mapKeyVal = map[mapKey];//This is the value part for the map's key.


          }
  }
Binita Bharati
  • 4,026
  • 1
  • 34
  • 22
2

This is an old post, but one way I can think of is

const someMap = { a: 1, b: 2, c: 3 };
Object.keys(someMap)
.map(key => 'key is ' + key + ' value is ' + someMap[key]);

Should this way of iterating be used? Are there any issues with this approach?

Ketu
  • 1,428
  • 2
  • 11
  • 28
1

For lopping through a Hashmap you need to fetch the keys and values.

const new_Map = new Map();

for (const [key, value] of new_Map.entries()) {
   console.log(`The key is ${key} and value is ${value}`);
}

It should work with keys and values of hashmap in key and value.

1

var a_hashMap = {a:1,b:2,c:3};

for (var key in a_hashMap) {
    console.log('Key: ' + key + '. Value: ' + a_hashMap[key]);
}
Matt
  • 30,192
  • 23
  • 73
  • 87