# Check if the Value is present in an array

**URL:** <https://discourse.nodered.org/t/check-if-the-value-is-present-in-an-array/68370>\
**Category:** General\
**Created:** [29 September 2022 04:48 UTC](https://discourse.nodered.org/t/check-if-the-value-is-present-in-an-array/68370 "2022-09-29T04:48:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ganesh](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@Ganesh](https://discourse.nodered.org/u/Ganesh)\
**Post date:** [29 September 2022 04:48 UTC](https://discourse.nodered.org/t/check-if-the-value-is-present-in-an-array/68370/1 "2022-09-29T04:48:56Z")

</div>

Hi

```auto
var Zone = msg.payload;
var Value = '34:80:73:90:d9:82';
const xx=[];

var x;
var y;
var radius;
var color;

if (Value == Zone) {
    if(!xx.includes(45)){
        x = 45;
        y = 70;
        radius = 6;
        color = '#7D6608';
        xx.push(x);
     }
    if(!xx.includes(55)){
        x = 55;
        y = 70;
        radius = 6;
        color = '#7D6608';
        xx.push(x);
    }
    if(!xx.includes(85)){
        x = 85;
        y = 70;
        radius = 6;
        color = '#7D6608';
        xx.push(x);
    }
    }
    else {
    x = x;
    y = y;
    radius = radius;
    color = 'red';
    }

msg.lat = x
msg.long = y
msg.radius = radius
msg.color = color

return {
    payload: [{
        x: msg.lat,
        y: msg.long,
        radius: msg.radius,
        color: msg.color
    }]
}

```

In the above function, I am comparing two conditions.  
First, if the incoming value and defined value are the same, it should send x and y values.  
2nd when the function is executed. It should send the first set of values and store them in an array. When it is triggered a second time, it should compare whether the value x is present in the array. If it is, it should send the next set of values.  
Help is needed !

```auto
[
    {
        "id": "ddbb8807e1dafec2",
        "type": "inject",
        "z": "35bc2aafbcc33d8e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "34:80:73:90:d9:82",
        "payloadType": "str",
        "x": 190,
        "y": 140,
        "wires": [
            [
                "0b059a56507de4ae"
            ]
        ]
    },
    {
        "id": "0b059a56507de4ae",
        "type": "function",
        "z": "35bc2aafbcc33d8e",
        "name": "function 88",
        "func": "var Zone = msg.payload;\nvar Value = '34:80:73:90:d9:82';\nconst xx=[];\n\nvar x;\nvar y;\nvar radius;\nvar color;\n\nif (Value == Zone) {\n if(!xx.includes(45)){\n x = 45;\n y = 70;\n radius = 6;\n color = '#7D6608';\n xx.push(x);\n }\n if(!xx.includes(55)){\n x = 55;\n y = 70;\n radius = 6;\n color = '#7D6608';\n xx.push(x);\n }\n if(!xx.includes(85)){\n x = 85;\n y = 70;\n radius = 6;\n color = '#7D6608';\n xx.push(x);\n }\n }\n else {\n x = x;\n y = y;\n radius = radius;\n color = 'red';\n }\n\nmsg.lat = x\nmsg.long = y\nmsg.radius = radius\nmsg.color = color\n\nreturn {\n payload: [{\n x: msg.lat,\n y: msg.long,\n radius: msg.radius,\n color: msg.color\n }]\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 270,
        "y": 180,
        "wires": [
            [
                "226552e7f5101b02",
                "6e9af15df22cc7a3"
            ]
        ]
    },
    {
        "id": "6e9af15df22cc7a3",
        "type": "debug",
        "z": "35bc2aafbcc33d8e",
        "name": "debug 84",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 480,
        "y": 180,
        "wires": []
    }
]

```

---

<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:** [29 September 2022 05:53 UTC](https://discourse.nodered.org/t/check-if-the-value-is-present-in-an-array/68370/2 "2022-09-29T05:53:36Z")

</div>

> [@Ganesh](#):
>
> First, if the incoming value and defined value are the same, it should send x and y values.  
> 2nd when the function is executed. It should send the first set of values and store them in an array. When it is triggered a second time, it should compare whether the value x is present in the array

Every time your function runs, the array `xx` is created. If you wish for `xx` to be maintained between executions you will need to restore it at the beginning and store it at the end of the function. For this, you can use context.

---

<div class="post-metadata">

**Author:** ![Ganesh](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@Ganesh](https://discourse.nodered.org/u/Ganesh)\
**Post date:** [29 September 2022 06:13 UTC](https://discourse.nodered.org/t/check-if-the-value-is-present-in-an-array/68370/3 "2022-09-29T06:13:38Z")

</div>

Can you send an example?  
It would be helpful.

---

<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:** [29 September 2022 06:20 UTC](https://discourse.nodered.org/t/check-if-the-value-is-present-in-an-array/68370/4 "2022-09-29T06:20:44Z")

</div>

There are hundreds of complete examples all around the forum. There's even a section in the docs on using context.

- [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)
- [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions)

---

<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:** [13 October 2022 06:20 UTC](https://discourse.nodered.org/t/check-if-the-value-is-present-in-an-array/68370/5 "2022-10-13T06:20:59Z")

</div>

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