final ScriptableObject spMap = new NativeMap();
Map<String,Object> javaMap = Map.of("a",10,"b",20,"c",35,"dee",500, "last","I'm the last key!");
javaMap.forEach( (a,b)->spMap.put(a, spMap, b) );
try {
Context.enter();
YOUR_PROGRAM_GLOBAL_SCOPE.put("dynMap", YOUR_PROGRAM_GLOBAL_SCOPE, Context.javaToJS(javaMap, YOUR_PROGRAM_GLOBAL_SCOPE));
} finally {
Context.exit();
}
Javascript (
bp.log.info is a logger, think of it as println() or console.log )
--------------
if ( dynMap ) {
for ( let d in dynMap ) {
}
}
Result
---------
[BP][Info] b: 20
[BP][Info] c: 35
[BP][Info] dee: 500
[BP][Info] last: I'm the last key!
[BP][Info] a: 10
You can try to remove the call to Context.javaToJS and just place the object in the scope, since it's a native object. Also, there may be better ways of doing this.... but this works.