# Array compare not going well

**URL:** <https://discourse.nodered.org/t/array-compare-not-going-well/61163>\
**Category:** General\
**Created:** [12 April 2022 10:46 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163 "2022-04-12T10:46:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [12 April 2022 10:46 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/1 "2022-04-12T10:46:24Z")

</div>

Hello everyone,

i would like to compare an array, when its 1,1 its should give 1 as output and when its 0,0 it should give a 0. Every other combination should give an error message.

i tried this as you can see bellow but it doesnt work ... can someone help pls?

```auto
[{"id":"849ba83b943e3dea","type":"debug","z":"2ecd18bc023a4c74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1710,"y":600,"wires":[]},{"id":"093db9db80f0ac06","type":"function","z":"2ecd18bc023a4c74","name":"","func":"//Waarden toekennen\nStatus= msg.payload\n\n\n//Bewerkingen doen met waarden.\nif (Status === [0,0]) {\n Status =0;\n Statusbericht=\"\"\n}\n \nelse if (Status === [1,1]) {\n Status =1;\n Statusbericht=\"\"\n}\n \nelse {\nStatusbericht =\"Er is een technische fout opgetreden!\";\nStatus= \"\";\n}\n\n//Weergeven in console\nvar msg1 = {payload:Status}\nvar msg2 = {payload:Statusbericht}\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1540,"y":620,"wires":[["849ba83b943e3dea"],["e40a0efd73d202c1"]]},{"id":"e40a0efd73d202c1","type":"debug","z":"2ecd18bc023a4c74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1710,"y":640,"wires":[]}]

```

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 April 2022 11:34 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/2 "2022-04-12T11:34:06Z")

</div>

```auto
const Status = msg.payload;
if(!Array.isArray(Status) || Status.length !== 2) {
  node.error("ERROR - payload MUST be an array with 2 elements!", msg);
  return;
}

let result = "OK";
if( Status[0] === 0 && Status[1] === 0 ) {
  //both are 0
  msg.payload = 0;
} else if( Status[0] === 1 && Status[1] === 1 ) {
  //both are 1
  msg.payload = 1;
} else {
  result = `Status ${JSON.stringify(Status)} is not valid`;
}

const msg2 = { payload: result }
return [msg, msg2];

```

👆 UNTESTED - but should be enough hints to put you on the right track.

* * *

EDIT:

The reason `Status` will NEVER equal `[1,1]` is because JS compares objects by reference (e.g. compares the memory addresses of the 2 objects for equality). Since `Status` is 1 object and `[0,0]` is an entirely different object, their addresses in memory are different. PS, yes, an array is an `object` (try adding `node.warn(typeof [0,0])` to your code);

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [12 April 2022 11:39 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/3 "2022-04-12T11:39:27Z")

</div>

This seems to work too

```auto
let Status = msg.payload;
if (JSON.stringify(Status) === JSON.stringify([0,0])){
    msg.payload = "zero";
}
else if(JSON.stringify(Status) === JSON.stringify([1,1])){
    msg.payload = "one";
}
else {
    msg.payload = "neither";
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 April 2022 11:41 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/4 "2022-04-12T11:41:58Z")

</div>

That was very similar to my initial response (un posted) but changed it because if `msg.payload` was an array with 100000 elements, thats a whole lot of stringifying for no reason 🙂

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [12 April 2022 11:48 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/5 "2022-04-12T11:48:02Z")

</div>

Hmm thats extreme testing! I only considered msg.payload = timestamp instead of array.

Of course you don't need to stringify [0,0], you can substitute "[0,0]" on the RHS of the expression.

@Olivia is there a reason why you don't use const, let or var for your declaration of Status?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 April 2022 11:50 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/6 "2022-04-12T11:50:33Z")

</div>

> [@jbudd](#):
>
> Hmm thats extreme testing!

It is, but I always use the mantra "if it can happen, it will" - I'm a fairly defensive programmer. Not necessarily better, just defensive 😉

---

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [12 April 2022 11:53 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/7 "2022-04-12T11:53:38Z")

</div>

because i don't know a lot about node-red 🙂

---

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [12 April 2022 11:56 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/8 "2022-04-12T11:56:26Z")

</div>

Thanks a lot Steve !

---

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [12 April 2022 12:09 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/9 "2022-04-12T12:09:38Z")

</div>

@Steve-Mcl is there a way that msg 2 would only send a text message when there is an error because i work with a notification on the screen and it shouldnt pop up when its okay... i tried moving the return msg2 into the else but that doesnt seem to work either

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 April 2022 12:17 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/10 "2022-04-12T12:17:33Z")

</div>

> [@Olivia](#):
>
> is there a way that msg 2 would only send a text message when there is an error

return `null` instead of an actual object.

```auto
const Status = msg.payload;
if(!Array.isArray(Status) || Status.length !== 2) {
  const errMsg = "ERROR - payload MUST be an array with 2 elements!"
  node.error(errMsg , msg);
  return [null, {payload: errMsg }]; //only send to output pin 2
}

let msg2 = null; //set nnull by default
if( Status[0] === 0 && Status[1] === 0 ) {
  msg.payload = 0; //both are 0
} else if( Status[0] === 1 && Status[1] === 1 ) {
  msg.payload = 1; //both are 1
} else {
   msg2 = { payload: `Status ${JSON.stringify(Status)} is not valid` };
}

return [msg, msg2]; //when msg2 is null, nothing will be output on pin2

```

It is all documented here: [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#writing-a-function)

---

<div class="post-metadata">

**Author:** ![Olivia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/olivia/32/57161_2.png) [@Olivia](https://discourse.nodered.org/u/Olivia)\
**Post date:** [12 April 2022 12:21 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/11 "2022-04-12T12:21:47Z")

</div>

Wow, its just that easy 😮

Thanks

---

<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:** [26 April 2022 12:22 UTC](https://discourse.nodered.org/t/array-compare-not-going-well/61163/12 "2022-04-26T12:22:38Z")

</div>

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