Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  11] [ 2]  / answers: 1 / hits: 122780  / 13 Years ago, tue, july 19, 2011, 12:00:00

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?


More From » hashmap

 Answers
16
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


[#91119] Sunday, July 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyquandaquanl

Total Points: 122
Total Questions: 109
Total Answers: 101

Location: South Georgia
Member since Sun, Aug 8, 2021
3 Years ago
;