Iterating through a map using Reflect.fields end up with an "h"?

I am trying to iterate through a map and getting odd result

class Test {
  static function main() {
    var m = new Map<String,String>();
    m.set("a", "1");
    m.set("b", "2");
    m.set("c", "3");
    
    trace(m);
    for (f in Reflect.fields(m)) {
			trace(f);
    }
  }
}

The second trace call only outputs “h”.

What am I doing wrong?

I figured it out, I was not supposed to use Reflect.fields on maps. Because I can iterate though them on their own via keys()

class Test {
  static function main() {
    var m = new Map<String,String>();
    m.set("a", "1");
    m.set("b", "2");
    m.set("c", "3");
    
    for (i in m.keys()) {
      trace(i);
      trace(m[i]);
	}
  }
}

A couple more ways to iterate map you might find useful

// iterate over values
for (value in m) {
	trace(value);
}

// iterate over keys and values
for (key => value in m) {
	trace('$key => $value');
}

You can also initialize with the => syntax:

var m = [
    "a" => "1",
    "b" => "2",
    "c" => "3",
];
3 Likes

And get/set keys like on arrays: map["a"] = "1";