Loop through a 'Hashmap' in JavaScript

后端 未结 8 842
逝去的感伤
逝去的感伤 2020-12-08 09:37

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 fi

相关标签:
8条回答
  • 2020-12-08 10:23
    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"

    0 讨论(0)
  • 2020-12-08 10:23

    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]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题