通过哈希循环

时间:2012-10-24 21:12:53

标签: haxe

在下面的代码中,字段长度始终为0,我很确定其中有一些键值对。

var fields = Reflect.fields(_commandMap);
trace("mapping "+fields.length);

1 个答案:

答案 0 :(得分:1)

您无法在哈希值中以数组形式访问值。

这是一个哈希

var a = new Hash();
a.set("hello", 0);
a.set("bonjour", 1);
a.set("ohai", 2);

您可以通过以下方式访问值/键:

for (value in a)
{
    trace(value); //Will trace 0, 1, 2 (no assurance that it will be in that order)
}

for (key in a.keys())
{
    trace(key); //Will trace hello, bonjour, ohai (no assurance that it will be in that order)
}

如果您想将哈希转换为数组,请使用Lambda

var valueArray = Lambda.array(a);
trace(valueArray[0]); //can be 0, 1 or 2

//since keys() returns an Iterator, not an Iterable, we cannot use Lambda here...
var keyArray = [];
for (key in a.keys()) keyArray.push(key);
trace(keyArray[0]); //can be hello, bonjour or ohai