Check if the Value is present in an array

Hi

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 !

[
    {
        "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": []
    }
]

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.

Can you send an example?
It would be helpful.

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

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