node.js – Node RED – function to add object to array
Im trying to create a function that will add a new object to an array. the Array and Object is returned in the format showed in the picture
namely function 1 and function 7 write the array and object in to global variables that is called up in function 6. I need to add the object to the array and return the new/updated array.
I have tried multiple different ways without luck, push(), concat() etc.
[
{
"id": "a45c52c2a533d8cd",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "165f8bf716ae6b6e",
"type": "tak",
"z": "a45c52c2a533d8cd",
"name": "TAK",
"x": 270,
"y": 200,
"wires": [
[
"7305075b4b224054",
"382996e7cc068a18"
],
[],
[]
]
},
{
"id": "2d65c1a680b00ce3",
"type": "udp in",
"z": "a45c52c2a533d8cd",
"name": "",
"iface": "",
"port": "4444",
"ipv": "udp4",
"multicast": "false",
"group": "",
"datatype": "utf8",
"x": 80,
"y": 240,
"wires": [
[
"165f8bf716ae6b6e"
]
]
},
{
"id": "382996e7cc068a18",
"type": "https-node",
"z": "a45c52c2a533d8cd",
"name": "",
"method": "GET",
"ret": "obj",
"paytoqs": "ignore",
"url": "http://test.cl1.dev234.cyou/test?force=true",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"senderr": false,
"x": 510,
"y": 100,
"wires": [
[
"10e946950aa09395"
]
]
},
{
"id": "f70a541798df5761",
"type": "debug",
"z": "a45c52c2a533d8cd",
"name": "debug 2",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 740,
"y": 60,
"wires": []
},
{
"id": "10e946950aa09395",
"type": "function",
"z": "a45c52c2a533d8cd",
"name": "function 8",
"func": "var newMsg = { payload: msg.payload.items };\nglobal.set(\"test2\", newMsg); // to store a variable\nreturn newMsg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 460,
"y": 280,
"wires": [
[
"a6982fbaddcc49b8"
]
]
},
{
"id": "a6982fbaddcc49b8",
"type": "join",
"z": "a45c52c2a533d8cd",
"name": "",
"mode": "custom",
"build": "array",
"property": "payload",
"propertyType": "msg",
"key": "payload",
"joiner": "[]",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "2",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "num",
"reduceFixup": "",
"x": 630,
"y": 360,
"wires": [
[
"1934a672eb7bf91b",
"c44d1b45359f4f2f"
]
]
},
{
"id": "7305075b4b224054",
"type": "function",
"z": "a45c52c2a533d8cd",
"name": "function 9",
"func": "var lat = msg.payload.event.point._attributes.lat;\nvar lon = msg.payload.event.point._attributes.lon;\nvar callsign = msg.payload.event.detail.contact._attributes.callsign;\nvar timestamp = msg.payload.event._attributes.time;\nmsg.payload = {\n \"drawable\": \"point\",\n \"timestamp\": \"2023-05-17T14:03:07Z\",\n \"timespan\": {\n \"begin\": \"0001-01-01T00:00:00Z\",\n \"end\": \"0001-01-01T00:00:00Z\"\n },\n\n \"modifiers\": {\n \"t\": \"TEST\",\n \"w\": \"17140307ZMAY2023\"\n },\n \"label\": callsign,\n \"symbol\": \"app6d:10031000001308000000\",\n \"x\": 57.0960971,\n \"y\": 19.1526662\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 400,
"y": 340,
"wires": [
[
"da776df464fe2022",
"c44c0c2645a7ed15"
]
]
},
{
"id": "1934a672eb7bf91b",
"type": "https-node",
"z": "a45c52c2a533d8cd",
"name": "",
"method": "POST",
"ret": "txt",
"paytoqs": "ignore",
"url": "http://127.0.0.1:8080/test",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"senderr": false,
"x": 850,
"y": 220,
"wires": [
[]
]
},
{
"id": "da776df464fe2022",
"type": "function",
"z": "a45c52c2a533d8cd",
"name": "function 11",
"func": "var item = msg.payload;\n\nmsg.payload = []; // create an empty array\nmsg.payload[0] = {}; // create an empty object as first item\nmsg.payload[0] = item;\n//var newMsg = { payload: msg.payload };\n//global.set(\"tak2\", item); // to store a variable\n//return newMsg;\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 520,
"y": 500,
"wires": [
[
"a6982fbaddcc49b8"
]
]
},
{
"id": "c44d1b45359f4f2f",
"type": "function",
"z": "a45c52c2a533d8cd",
"name": "function 12",
"func": "var a = [];\n//let name3 = {};\n\nlet name = global.get(\"test2\"); //to retrieve a variable \nlet name2 = global.get(\"tak\"); //to retrieve a variable \nlet test = name.split(\"]\").pop()\nreturn test;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 800,
"y": 420,
"wires": [
[
"f70a541798df5761"
]
]
},
{
"id": "c44c0c2645a7ed15",
"type": "function",
"z": "a45c52c2a533d8cd",
"name": "function 13",
"func": "var newMsg = { payload: msg.payload };\nglobal.set(\"tak\", newMsg); // to store a variable\nreturn newMsg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 380,
"y": 400,
"wires": [
[]
]
}
]
any help appriciated.
thanks
Read more here: Source link