# How to compare full arrays in node red

**URL:** <https://discourse.nodered.org/t/how-to-compare-full-arrays-in-node-red/66846>\
**Category:** General\
**Tags:** function-node\
**Created:** [24 August 2022 11:32 UTC](https://discourse.nodered.org/t/how-to-compare-full-arrays-in-node-red/66846 "2022-08-24T11:32:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FranPanito](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/franpanito/32/58616_2.png) [@FranPanito](https://discourse.nodered.org/u/FranPanito)\
**Post date:** [24 August 2022 11:32 UTC](https://discourse.nodered.org/t/how-to-compare-full-arrays-in-node-red/66846/1 "2022-08-24T11:32:58Z")

</div>

Hi, I´m new to node red and Javascript and got a problem that maybe somebody can help me to short out.

I have a flow that in a point writes three 16bit status arrays in a database. It works ok but I´m trying to filter so it only should be writing a new row when a bit change in the incoming new msg.  
To do so I´ve implemented a function node with the following JS code :

```auto
var old_State1;
var old_State2;
var old_State3;

if (msg.payload.State1 != old_State1) {
    old_State1 = msg.payload.State1;
    return msg;
}
if (msg.payload.State2 != old_State1){
    old_State2 = msg.payload.State2;
    return msg; 
} 
if (msg.payload.State3 != old_State1){
    old_State3 = msg.payload.State2;
    return msg;
}

```

Also at "On Start" section I´ve added the following :

```auto
old_State1 = [];
old_State2 = [];
old_State3 = [];

```

As you can imagine this is nos working so I gess I´m doing something wrong.

My node-red version is 2.2.2 and my node.js version is 16.16.0

Any help would be so much appreciated 🙂

---

<div class="post-metadata">

**Author:** ![FranPanito](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/franpanito/32/58616_2.png) [@FranPanito](https://discourse.nodered.org/u/FranPanito)\
**Post date:** [24 August 2022 11:57 UTC](https://discourse.nodered.org/t/how-to-compare-full-arrays-in-node-red/66846/2 "2022-08-24T11:57:30Z")

</div>

This is the array structure just in case it helps to clarify :

 ![State arrays nodered](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9a73ff268a9a354b0bcfeef64c73689d7db2a1b9.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [24 August 2022 12:26 UTC](https://discourse.nodered.org/t/how-to-compare-full-arrays-in-node-red/66846/3 "2022-08-24T12:26:38Z")

</div>

Try using the **Filter node**.. it should filter (block) any repeated msgs and not let them pass unless there is a change. Seems to work even for deep nested objects .. so no need to check them with a Function node.

Example Flow

```auto
[{"id":"e19bedf4f11386ff","type":"rbe","z":"54efb553244c241f","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":390,"y":1720,"wires":[["0422d9f7a02b816e"]]},{"id":"fa3b512570800543","type":"inject","z":"54efb553244c241f","name":"000","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"status1\":{\"bits\":[{\"bit1\":0},{\"bit2\":0},{\"bit3\":0}]},\"status2\":{\"bits\":[{\"bit1\":0},{\"bit2\":0},{\"bit3\":0}]}}","payloadType":"json","x":190,"y":1680,"wires":[["e19bedf4f11386ff"]]},{"id":"402c098817a3ee9f","type":"inject","z":"54efb553244c241f","name":"111","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"status1\":{\"bits\":[{\"bit1\":1},{\"bit2\":1},{\"bit3\":1}]},\"status2\":{\"bits\":[{\"bit1\":1},{\"bit2\":1},{\"bit3\":1}]}}","payloadType":"json","x":190,"y":1780,"wires":[["e19bedf4f11386ff"]]},{"id":"0422d9f7a02b816e","type":"debug","z":"54efb553244c241f","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":1720,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![FranPanito](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/franpanito/32/58616_2.png) [@FranPanito](https://discourse.nodered.org/u/FranPanito)\
**Post date:** [24 August 2022 12:39 UTC](https://discourse.nodered.org/t/how-to-compare-full-arrays-in-node-red/66846/4 "2022-08-24T12:39:16Z")

</div>

Many thanks!!!,

It works ok with filter node, sometimes the solution is in front of yourself and don´t see it !!

Best regards 🙂

---

<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:** [7 September 2022 12:39 UTC](https://discourse.nodered.org/t/how-to-compare-full-arrays-in-node-red/66846/5 "2022-09-07T12:39:19Z")

</div>

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