# Syntax in ECMA rule for "members.findFirst"

**URL:** <https://community.openhab.org/t/syntax-in-ecma-rule-for-members-findfirst/117201>\
**Category:** Scripts & Rules\
**Tags:** rule, next-gen-rules, ecma\
**Created:** [February 16, 2021, 4:05pm UTC](https://community.openhab.org/t/syntax-in-ecma-rule-for-members-findfirst/117201 "2021-02-16T16:05:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johannesbonn](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/b782af/32.png) [@johannesbonn](https://community.openhab.org/u/johannesbonn)\
**Post date:** [February 16, 2021, 4:05pm UTC](https://community.openhab.org/t/syntax-in-ecma-rule-for-members-findfirst/117201/1 "2021-02-16T16:05:07Z")

</div>

Hello,

i have a rule with the code line:

> val windowsitem = gWindowsfind.members.findFirst[id | id.name == windowsname]

Can somebody help me to find the right code syntax for my ECMA rule. I have no idea where I can find the required information.

Thank you!

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 16, 2021, 4:40pm UTC](https://community.openhab.org/t/syntax-in-ecma-rule-for-members-findfirst/117201/2 "2021-02-16T16:40:39Z")

</div>

First of all, you have to pull the Item from the Item Registry.

```javascript
var gWindowsfind = ir.getItem("gWindowsfind");

```

There are two paths you can take.

1. Convert the Java ArrayList that members returned to a native JavaScript Array and use the built in JavaScript stuff to manipulate it. However, Nashorn is ECMAScript 5.1 so Arrays.find doesn’t exist so you’ll have to implement the find function yourself. Yuck.

2. Keep it as a Java ArrayList and use the Java native stuff to manipulate it. This requires using the [Java Stream API](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html#findFirst()). Luckily this works almost exactly like it does in Rules DSL (I’m pretty sure it’s what is used behind the scenes) but there are a few differences. For example, findFirst doesn’t work the same way (it just returns the first element wrapped in an Optional object).

```javascript
val windowsitem = gWindowsfind
                    .members
                    .stream()
                    .filter(function(id) { return id.name == windowsname; })
                    .findFirst()
                    .get();

```

I need to update the Working with Groups in Rules page but have other higher priority stuff to deal with first.

---

<div class="post-metadata">

**Author:** ![Sascha\_Billian](https://community.openhab.org/user_avatar/community.openhab.org/sascha_billian/32/17056_2.png) [@Sascha\_Billian](https://community.openhab.org/u/Sascha_Billian)\
**Post date:** [April 26, 2021, 6:53am UTC](https://community.openhab.org/t/syntax-in-ecma-rule-for-members-findfirst/117201/3 "2021-04-26T06:53:53Z")

</div>

Sorry to push that up again, but did you find a way to do this with ECMA? I’m really struggling with that right now and it would be good to have an example 😉

Edit: Got it working by using the exact syntac from Rich. So i just didn’t understand the post in the first time
