I'm trying to do scripting with Javascript in a Java program. I haven't found a way to iterate through a Java collection in Javascript. If I call the iterator() method for the collection, I get the method names instead of the elements.
Here's a sample code:
function getValue(row, components) {
var apartment = components.get(0);
var rooms = apartment.getRooms();
for (var room in rooms.iterator()) {
println(room);
}
return rooms.toString();
}
The apartment.getRooms() returns a collection of rooms. When I study the value returned by this function, I know that its content are correct, but the values that get printed are the method names.
I invoke the Javascript from my Java program like this:
getInvocable().invokeFunction("getValue", row, components);
It seems that if I do the iteration as follows:
function getValue(row, components) {
var apartment = components.get(0);
var rooms = apartment.getRelated();
for (var iterator = rooms.iterator(); iterator.hasNext();) {
var room = iterator.next();
println(room);
}
return rooms.toString();
}
It works.
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