在JavaScript中循环使用'Hashmap' [英] Loop through a 'Hashmap' in JavaScript
问题描述
我正在使用此方法在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)
$ p $这意味着循环
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 toi
"这篇关于在JavaScript中循环使用'Hashmap'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!