在JavaScript中循环使用'Hashmap' [英] Loop through a 'Hashmap' in JavaScript

查看:96
本文介绍了在JavaScript中循环使用'Hashmap'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用方法在javascript中制作人造hashmaps。我所瞄准的是关键值对,实际运行时间并不重要。下面的方法工作正常。



是否有其他方式循环浏览?

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

我碰到一个问题,在第一个输出一串未定义的键之后键,当数组只包含一个条目时。我有一种感觉,因为代码是在使用i的循环中,即使我在调试时不应该发生这种情况。

任何人有任何想法?

解决方案

  for(var i in a_hashmap [i])

不正确。它应该是

  for(var i in a_hashmap)
a_hashmap 的属性,依次将每个属性名称分配给 i


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?

解决方案

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"

这篇关于在JavaScript中循环使用'Hashmap'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆