How to convert a Map<String,String> into a JavaScript Object

641 views
Skip to first unread message

der Raab

unread,
Jan 16, 2015, 1:25:08 PM1/16/15
to haxe...@googlegroups.com
Hi everybody,

I struggle with this very basic task: I need to convert a Map<String,String> into an basic JavaScript Object but the compile doesn't allow this:

var map : Map<String,String> = propertiesParser.getMap();
var object : Dynamic = {};

for ( key in map.keys() ) {

 
object[ key ] = map.get( key );
}


The compiler warns: String should be int on line object[ key ] = map.get( key );

Ivan Tivonenko

unread,
Jan 16, 2015, 1:36:07 PM1/16/15
to haxe...@googlegroups.com
Just do

Reflect.setField(object, key, map.get(key));

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

der Raab

unread,
Jan 16, 2015, 2:39:46 PM1/16/15
to haxe...@googlegroups.com, darkd...@darkdragon.com.ua
Thank you very much! But i wonder if there is another syntax? Without the static method overhead?

Ivan Tivonenko

unread,
Jan 16, 2015, 2:56:05 PM1/16/15
to haxe...@googlegroups.com
Reflect methods will be optimized for each platform as much as possible.
For javascript, as example, this will generate
object.key = ...

der Raab

unread,
Jan 16, 2015, 4:26:54 PM1/16/15
to haxe...@googlegroups.com, darkd...@darkdragon.com.ua
No, in JavaScript this get's generated:


var map = propertiesParser.getMap();
var object = { };
var $it0 = map.keys();
while( $it0.hasNext() ) {
 
var key = $it0.next();
 
Reflect.setField(object,key,map.get(key));
}


//With this reflect method:


Reflect.setField = function(o,field,value) {
 o
[field] = value;
};


So it would be better to have it inlined. Anyway - thank you! 
Message has been deleted

Jonas Malaco Filho

unread,
Jan 16, 2015, 6:46:08 PM1/16/15
to haxe...@googlegroups.com, darkd...@darkdragon.com.ua
setField should generally get inlined but, probably due to some underlying 'untyped' in Map.get(), that doesn't happen due to safety reasons; you should check haxe/issues/2179.

But all is not lost. If you assign the value to some var, setField ends up inlined: try.haxe.org/#42F04.

der Raab

unread,
Jan 17, 2015, 9:44:07 AM1/17/15
to haxe...@googlegroups.com, darkd...@darkdragon.com.ua
Great hint Jonas! This works:


Thank you very much!
Reply all
Reply to author
Forward
0 new messages