# Best way to replicate OR function?

**URL:** <https://discourse.nodered.org/t/best-way-to-replicate-or-function/19972>\
**Category:** General\
**Created:** [6 January 2020 16:22 UTC](https://discourse.nodered.org/t/best-way-to-replicate-or-function/19972 "2020-01-06T16:22:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [6 January 2020 16:22 UTC](https://discourse.nodered.org/t/best-way-to-replicate-or-function/19972/1 "2020-01-06T16:22:31Z")

</div>

Sorry for another noob question but I'm missing the typical OR function to test mulitple values in a function node. I was trying to avoid having to have lots of extra change nodes.

I want to test payload and change as required so "1",1,True,"ON" are changed to "ON" and "0",0,"false","OFF" are changed to "OFF"

I have done a lot of searching and it seemed that the switch command may fit the bill but it doesn't seem to work for me.

Is there a neat wat to do this in a function node please ?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 January 2020 16:30 UTC](https://discourse.nodered.org/t/best-way-to-replicate-or-function/19972/2 "2020-01-06T16:30:36Z")

</div>

There are various ways. I don't think the switch node will help much unless you want to learn JSONata which can be made to do this.

However, in a function, you either use a long-winded `if` statement or a more readable `switch` statement.

```javascript
const x = msg.payload
if ( x === "1" || x === 1 || x.toLowerCase() === 'on' .... ) { .... }

switch (x) {
    case '1':
    case 1:
    case 'ON':
    ....

        // do something
        break

    default:
        // do something different
}

```

Personally, I try to make sure that I rationalise all of my inputs **before** processing them so that I don't have to worry about this when dealing with actual logic.

There is a 3rd method (which would also be best in JSONata probably) which is to use an array and a filter function. More complex but the nice thing is that it never gets more complex no matter how many choices you end up with. I use that when processing the list of possible libraries available to uibuilder. The list comes from a file and could be of any arbitrary length.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [6 January 2020 16:33 UTC](https://discourse.nodered.org/t/best-way-to-replicate-or-function/19972/3 "2020-01-06T16:33:15Z")

</div>

If you want to do it with a standard node you can use the Change node for this sort of thing

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c7b78fcb17b16a8a5ea1768a42c05fe0ca6f8c1a.png)

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [6 January 2020 16:44 UTC](https://discourse.nodered.org/t/best-way-to-replicate-or-function/19972/4 "2020-01-06T16:44:13Z")

</div>

Thanks I was nearly there ... but tried to use switch (msg.payload) directly.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 January 2020 16:54 UTC](https://discourse.nodered.org/t/best-way-to-replicate-or-function/19972/5 "2020-01-06T16:54:15Z")

</div>

Just to say that Simon's reply is what I was referring to when saying that I try to rationalise input data early. It makes any further processing SOOO much easier.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [6 January 2020 17:04 UTC](https://discourse.nodered.org/t/best-way-to-replicate-or-function/19972/6 "2020-01-06T17:04:14Z")

</div>

Thanks to both of you for the very quick replies, I did already have a change node like that, but wanted one catch all function node.

The issue is that with a number of different devices requiring different outputs I have to send the correct payloads to and from switch nodes to correctly operate and display current status on dashboard. This function IS a way to rationalise things by adding this to the switch outputs I can now store the logical state ON or OFF in one global object with Device and State.

Thanks again much appreciated.
