I have the following method in the file test.js:
function avg(input, period) {
var output = [];
if (input === undefined) {
return output;
}
var i,j=0;
for (i = 0; i < input.length- period; i++) {
var sum =0;
for (j = 0; j < period; j++) {
//print (i+j)
sum =sum + input[i+j];
}
//print (sum + " -- " + sum/period)
output[i]=sum/period;
}
return output;
}
I want to pass an array from java to this function and to get the js output array in java. I used the following java code:
double[] srcC = new double[] { 1.141, 1.12, 1.331, 1.44, 1.751, 1.66, 1.971, 1.88, 1.191, 1.101 };
try {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("nashorn");
String location = "test.js";
engine.eval("load(\"" + location + "\");");
Invocable invocable = (Invocable) engine;
// double[] a = (double[]) invocable.invokeFunction("avg", srcC, 2);
System.out.println("obj " + invocable.invokeFunction("avg", srcC, 2));
} catch (Exception ex) {
LOGGER.error(ex.getLocalizedMessage());
}
I am able to see the output of the avg js function but I do not know how to get the js output arrays from the js avg function in java
Any support is appreciated.
Best Regards, Aurelian