# Compare two values in an array

**URL:** <https://discourse.nodered.org/t/compare-two-values-in-an-array/27747>\
**Category:** General\
**Created:** [4 June 2020 05:40 UTC](https://discourse.nodered.org/t/compare-two-values-in-an-array/27747 "2020-06-04T05:40:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Truman](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Peter\_Truman](https://discourse.nodered.org/u/Peter_Truman)\
**Post date:** [4 June 2020 05:40 UTC](https://discourse.nodered.org/t/compare-two-values-in-an-array/27747/1 "2020-06-04T05:40:01Z")

</div>

Hi - sorry to bother you with this - I'm new to Java and node red (and Object Oriented Code in general)

I have a join node that produces an array with 2 values

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/597491e2c00234169183fb536fe366d66d9297ef.png)

I'm just trying to write a function node that compares the two array values

Like this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/9166b0159993d6d88b38e1c417ab4e46a9d310b4.png)  
So - just comparing the two values and coming up with an appropriate message.

I'm not sure if I have failed to grasp some fundamental issue or if it's just a syntax thing?

Can anyone help steer me right on this? Thanks in advance.

---

<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:** [4 June 2020 07:03 UTC](https://discourse.nodered.org/t/compare-two-values-in-an-array/27747/2 "2020-06-04T07:03:27Z")

</div>

Your code should work.  
the warning triangles are just telling you to use === instead of ==.  
== equal values.  
=== equal values and types.  
[https://www.w3schools.com/js/js\_comparisons.asp](https://www.w3schools.com/js/js_comparisons.asp)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [4 June 2020 07:31 UTC](https://discourse.nodered.org/t/compare-two-values-in-an-array/27747/3 "2020-06-04T07:31:44Z")

</div>

What if both [0] and [1] are 1 ?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 June 2020 08:08 UTC](https://discourse.nodered.org/t/compare-two-values-in-an-array/27747/4 "2020-06-04T08:08:59Z")

</div>

And what if they are both zero (0)?

---

<div class="post-metadata">

**Author:** ![Peter\_Truman](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Peter\_Truman](https://discourse.nodered.org/u/Peter_Truman)\
**Post date:** [5 June 2020 03:43 UTC](https://discourse.nodered.org/t/compare-two-values-in-an-array/27747/5 "2020-06-05T03:43:04Z")

</div>

Thanks for your help - I've worked through this and have it working now - I'm posting the flow here in case anyone else runs into the same newbie question.

Thanks again

```auto
[{"id":"ab6ef931.883208","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"ed5f8b48.7c3ed8","type":"join","z":"ab6ef931.883208","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"msg:topic","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":330,"y":340,"wires":[["35fd9386.5c1dbc","ec4df093.156d4"]]},{"id":"e6a97bb1.dc6408","type":"debug","z":"ab6ef931.883208","name":"Compare","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":580,"y":340,"wires":[]},{"id":"4dcb46f1.9cd1c8","type":"delay","z":"ab6ef931.883208","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":180,"y":340,"wires":[["ed5f8b48.7c3ed8"]]},{"id":"ff7670a8.28ba2","type":"change","z":"ab6ef931.883208","name":"Cng_On2_1","rules":[{"t":"change","p":"payload","pt":"msg","from":"On","fromt":"str","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":80,"wires":[["ed5f8b48.7c3ed8"]]},{"id":"72b87980.9774f8","type":"change","z":"ab6ef931.883208","name":"Chg_Off2_0","rules":[{"t":"change","p":"payload","pt":"msg","from":"Off","fromt":"str","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":120,"wires":[["ed5f8b48.7c3ed8"]]},{"id":"f014bafb.c5d598","type":"inject","z":"ab6ef931.883208","name":"Switch ON","topic":"On","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":80,"wires":[["ff7670a8.28ba2"]]},{"id":"7441c2ac.c5f60c","type":"inject","z":"ab6ef931.883208","name":"Switch OFF","topic":"Off","payload":"Off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":120,"wires":[["72b87980.9774f8"]]},{"id":"2ac3b51a.69e85a","type":"inject","z":"ab6ef931.883208","name":"Lamp On","topic":"1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":160,"wires":[["4dcb46f1.9cd1c8"]]},{"id":"af3a9129.66323","type":"inject","z":"ab6ef931.883208","name":"Lamp Off","topic":"0","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":200,"wires":[["4dcb46f1.9cd1c8"]]},{"id":"35fd9386.5c1dbc","type":"debug","z":"ab6ef931.883208","name":"Joined","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":450,"y":300,"wires":[]},{"id":"ec4df093.156d4","type":"function","z":"ab6ef931.883208","name":"Comp","func":"if (msg.payload[0] === 0 && msg.payload[1] == 0) {\n msg.payload = \"Output is OFF\";\n}\n\nif (msg.payload[0] === 1 && msg.payload[1] == 1) {\n msg.payload = \"Output is ON\";\n}\n\nif (msg.payload[0] === 0 && msg.payload[1] == 1) {\n msg.payload = \"Output On - should be OFF\";\n}\n\nif (msg.payload[0] === 1 && msg.payload[1] == 0) {\n msg.payload = \"Output OFF - should be ON\";\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"x":450,"y":340,"wires":[["e6a97bb1.dc6408"]]}]

```

---

<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:** [19 June 2020 03:43 UTC](https://discourse.nodered.org/t/compare-two-values-in-an-array/27747/6 "2020-06-19T03:43:07Z")

</div>

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