# Switch or function (or something else)?

**URL:** <https://discourse.nodered.org/t/switch-or-function-or-something-else/52416>\
**Category:** General\
**Created:** [19 October 2021 12:31 UTC](https://discourse.nodered.org/t/switch-or-function-or-something-else/52416 "2021-10-19T12:31:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk1blacklimited](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mk1blacklimited/32/49612_2.png) [@mk1blacklimited](https://discourse.nodered.org/u/mk1blacklimited)\
**Post date:** [19 October 2021 12:31 UTC](https://discourse.nodered.org/t/switch-or-function-or-something-else/52416/1 "2021-10-19T12:31:48Z")

</div>

New to NR, so this is probably easy...🙂

I've got a payload that includes the field "device\_id" which is an integer. I need to check if this device\_id is included in a list of device\_id's (an array perhaps), about 15 total. If so the flow should stop, else it should continue. I've been stuck trying out different options using switch and function, but I've not been able to succeed.

A bit of help would be much appreciated!

---

<div class="post-metadata">

**Author:** ![garrett](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/garrett/32/47697_2.png) [@garrett](https://discourse.nodered.org/u/garrett)\
**Post date:** [19 October 2021 13:02 UTC](https://discourse.nodered.org/t/switch-or-function-or-something-else/52416/2 "2021-10-19T13:02:53Z")

</div>

use a function node:

```auto
const myIds = [1,2,3,4]

if (myIds.find(myId => myId == msg.payload.device_id)) {
 node.send(msg)
}

```

---

<div class="post-metadata">

**Author:** ![mk1blacklimited](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mk1blacklimited/32/49612_2.png) [@mk1blacklimited](https://discourse.nodered.org/u/mk1blacklimited)\
**Post date:** [19 October 2021 13:23 UTC](https://discourse.nodered.org/t/switch-or-function-or-something-else/52416/3 "2021-10-19T13:23:15Z")

</div>

See, I said it was easy 🙂 Thanks, now off to check if this will work for me!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 October 2021 13:55 UTC](https://discourse.nodered.org/t/switch-or-function-or-something-else/52416/4 "2021-10-19T13:55:04Z")

</div>

Just to note that the code has a syntax error after .device\_id, missing )

@mk1blacklimited you could also use includes().

```auto
const myIds = [1,2,3,4]
if (myIds.includes(msg.payload.device_id)) {
 return msg;
}
return null;

```

---

<div class="post-metadata">

**Author:** ![mk1blacklimited](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mk1blacklimited/32/49612_2.png) [@mk1blacklimited](https://discourse.nodered.org/u/mk1blacklimited)\
**Post date:** [19 October 2021 16:12 UTC](https://discourse.nodered.org/t/switch-or-function-or-something-else/52416/5 "2021-10-19T16:12:52Z")

</div>

Thanks a lot @garrett and @E1cid, worked like a charm!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 December 2021 16:13 UTC](https://discourse.nodered.org/t/switch-or-function-or-something-else/52416/6 "2021-12-18T16:13:02Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
