Javascript rule error with short notations


I am currently playing around with Javascript (OH 3.0.1). I am trying to use the filter function with the short notation on an array:

var items = itemRegistry.getItems('Kitchen_.*').filter(item => item.hasTag("Light"));

In the log openhab complains with the following:

Script execution of rule with UID ‘6e7f0ec618’ failed: :8:62 Expected , but found =>

var items = itemRegistry.getItems(‘Kitchen_.*’).filter(item => item.hasTag(“Light”));
^ in at line number 8 at column number 62

I am wondering what I am doing wrong or is there some issue with the parser for Javascript?


itemsRegistry is a Java Object. getItems returns a Java java.util.List Object, not a JavaScript list. So the JavaScript stuff won’t work. You’ll either need to convert it to a JavaScript list so you can filter it in the usual JavaScript ways, or filter it using Java’s stream API.

Convert to a JavaScript List and filter:

var items = Java.from(itemRegistry.getItems('Kitchen_.*')).filter(item => item.hasTag("Light"));

Note that items will be a JavaScript Array

To use Java’s stream API:

var items = itemRegistry.getItems('Kitchen_.*').stream().filter(function(item) { return item.hasTag("Light"); });

items will be a java.util.List and not a JavaScript Array.

Note I just typed these two examples in. There may be a typo.