How can I convert JS array to native array ? In Rhino conversion looked like (Scala code):
val eng = (new javax.script.ScriptEngineManager).getEngineByName("JavaScript")
val obj = eng.eval("[1,2,3,4]")
val arr = obj.asInstanceOf[sun.org.mozilla.javascript.internal.NativeArray]
In Nashorn NativeArray absent, and I can't find any documentation on conversion.
From Java (and Scala), you can also invoke convert
method on jdk.nashorn.api.scripting.ScriptUtils
class. E.g. from Java:
import jdk.nashorn.api.scripting.ScriptUtils;
...
int[] iarr = (int[])ScriptUtils.convert(arr, int[].class)
my Scala is not too fluent, but I believe the equivalent is:
val iarr = ScriptUtils.convert(arr, Array[Int]).asInstanceOf(Array[Int])
Given a JavaScript array, you can convert it to a Java array using the Java.to() method in oracle nashorn engine which is available in jdk 8
Example
var data = [1,2,3,4,5,6];
var JavaArray = Java.to(data,"int[]");
print(JavaArray[0]+JavaArray[1]+JavaArray[2]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With