Newb Question: iterable Object like in JS/AS3

Hi, what´s the easiest way to get an iterable object as known from JS and AS3. I want to do this:

var obj = { foo:123, foo2:"somebar", boolfoo:false };
for (v in obj) {
trace(v, obj[v]);
}

thx

var obj = { foo:123, foo2:"somebar", boolfoo:false };
var objKeys = Reflect.fields(obj);
for (key in objKeys) {
    trace(key, Reflect.getProperty(obj, key));
}
1 Like

Thanks flashup, but I don´t like to having to reflect the object every time I want to iterate it. Is there another Object type that would fit for Iteration like this?

var map:Map<String, Dynamic> = ["foo" => 123, "foo2" => "somebar", "boolfoo" => false];
for(key in map.keys()) {
    trace(key + " => " + map[key]);
}

[Reference]

1 Like

Perhaps we could build iteration and array access into “openfl.utils.Object”? That could be nice.

Like this (if it was implemented):

var obj:Object = { foo:123, foo2:"somebar", boolfoo:false };
for (v in obj) {
   trace(v, obj[v]);
}
2 Likes

That would be perfect for the AS3-crowd :smile:

Thanks to a little help, this now works advertised :slight_smile:

1 Like

So this is implemeted now?
Sorry, I´m not sure if I understand your post, I´m no native speaker :confused:

Yes, it is now in the latest OpenFL release:

import openfl.utils.Object;

...

var data:Object = { hello: "World", cool: 100 };

for (field in data) {
    
    trace (field);
    trace (data[field]);
    
}
2 Likes

Awesome! This should really be part of Haxe :stuck_out_tongue: (Yeah, I know there’s a really long debate about this issue)

Edit: By the way, is there an impact on performance wen using it?

It’s not as fast as working with static data types (obviously), but it’s the same performance as running Reflect.fields and Reflect.field over the object :success: