节点红色函数添加和删除对象从上下文数组

发布于 2025-02-07 03:34:32 字数 601 浏览 2 评论 0原文

我在节点红色函数中有一个for循环,即我从上下文中获得数组的函数的开始

var alarmArray = flow.get("alarmArray")

,以及我需要在数组上推动对象,但我没有在流程上下文阵列上进行pusch在本地数组

alarmArray.push({
        key: "Low air pressure",
        value: alarm1Cip
        });
   flow.set("alarm1CipOld",alarm1Cip);

和之后,在for循环中,我需要从上下文中删除对象,但是以我的方式将其从本地数组中删除,

for (var key in alarmArray){
    node.warn(key);
    msg.payload = {
    "title": alarmArray[key].key,
    "isActive":alarmArray[key].value
    };
node.send(msg)
delete alarmArray[key]
}

如何在上下文数组中设法添加和删除对象? 谢谢

I have a for loop in the node red function that a the start of the function I get the array from the context

var alarmArray = flow.get("alarmArray")

and after I need to push object on the array I have do this but I'm not pusching on the flow context array but in the local array

alarmArray.push({
        key: "Low air pressure",
        value: alarm1Cip
        });
   flow.set("alarm1CipOld",alarm1Cip);

and after in the for loop I need to remove the object from the context but in my way I remove it from local array

for (var key in alarmArray){
    node.warn(key);
    msg.payload = {
    "title": alarmArray[key].key,
    "isActive":alarmArray[key].value
    };
node.send(msg)
delete alarmArray[key]
}

how can I manage to add and remove object in the context array?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那些过往 2025-02-14 03:34:32

这里的问题是Nodejs是通过参考语言的通过。这意味着这里没有2个阵列,只有1个具有2个手柄(var alarmarrayflow.get.get('armardArray')

这意味着任何推动/删除的东西在“本地”副本上,也将在上下文中将/删除到副本上。

每次您从上下文中恢复它,然后在本地进行副本时,做您想做的事情的唯一方法是将数组的深层复制。

假设数组仅容纳简单的对象,则以下内容应起作用:

var alarmArray = JSON.parse(JSON.stringify(flow.get('alarmArray')))

The problem here is that NodeJS is a pass by reference language. This means that there are not 2 arrays here, only 1 that has 2 handles (var alarmArray and flow.get('alarmArray'))

This means that anything pushed/deleted on the "local" copy is also getting pushed/deleted to the copy in the context.

The only way to do what you want will be to make a deep copy of the array every time you recover it from the context and then work on the copy locally.

Assuming the array only holds simple objects then the following should work:

var alarmArray = JSON.parse(JSON.stringify(flow.get('alarmArray')))
绾颜 2025-02-14 03:34:32

如果要保存更改,则只需要在正确的上下文中重新加载突变的对象

flow.set('alarmArray', alarmArray)

If you want to save your changes you just have to reload the mutated object in the right context, overwriting the previous value with

flow.set('alarmArray', alarmArray)
等你爱我 2025-02-14 03:34:32

您最好使用global.set而不是flow.set
这可能会对您有所帮助。

[
{
    "id": "133329daaceb6bb3",
    "type": "tab",
    "label": "flow 3",
    "disabled": false,
    "info": "",
    "env": []
},
{
    "id": "c80bec9a15b703f6",
    "type": "inject",
    "z": "133329daaceb6bb3",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 140,
    "y": 140,
    "wires": [
        [
            "8687551e01cd2045"
        ]
    ]
},
{
    "id": "8687551e01cd2045",
    "type": "function",
    "z": "133329daaceb6bb3",
    "name": "msg_1",
    "func": "global.set(\"msg_1\",\"test\");\n\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 280,
    "y": 140,
    "wires": [
        []
    ]
},
{
    "id": "103b03c39c8d9cae",
    "type": "debug",
    "z": "133329daaceb6bb3",
    "name": "debug 29",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "statusVal": "",
    "statusType": "auto",
    "x": 500,
    "y": 300,
    "wires": []
},
{
    "id": "c043cba9f34ba574",
    "type": "inject",
    "z": "133329daaceb6bb3",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 140,
    "y": 180,
    "wires": [
        [
            "e674c706437cc0f3"
        ]
    ]
},
{
    "id": "e674c706437cc0f3",
    "type": "function",
    "z": "133329daaceb6bb3",
    "name": "msg_2 ",
    "func": "global.set(\"msg_2\",\"hello\");\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 280,
    "y": 180,
    "wires": [
        []
    ]
},
{
    "id": "11f8fb75d4c4e6c0",
    "type": "inject",
    "z": "133329daaceb6bb3",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "1",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 150,
    "y": 300,
    "wires": [
        [
            "1346e34e8c83b2dc"
        ]
    ]
},
{
    "id": "1346e34e8c83b2dc",
    "type": "function",
    "z": "133329daaceb6bb3",
    "name": "check if 2 msg set",
    "func": "var msg1 = global.get(\"msg_1\");\nvar msg2 = global.get(\"msg_2\");\n\nif(msg1 && msg2){\n    msg.payload=\"both message arrived!\";\n}\nelse{\n    msg.payload=\"Nope. Not yet.\";\n}\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 330,
    "y": 300,
    "wires": [
        [
            "103b03c39c8d9cae"
        ]
    ]
},
{
    "id": "62d522a844321ef9",
    "type": "inject",
    "z": "133329daaceb6bb3",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 540,
    "y": 140,
    "wires": [
        [
            "6d109daff443dade"
        ]
    ]
},
{
    "id": "6d109daff443dade",
    "type": "function",
    "z": "133329daaceb6bb3",
    "name": "reset",
    "func": "global.set(\"msg_1\",null);\nglobal.set(\"msg_2\",null);\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 690,
    "y": 140,
    "wires": [
        []
    ]
},
{
    "id": "4e1848c867860d5f",
    "type": "comment",
    "z": "133329daaceb6bb3",
    "name": "Here gonna auto-run",
    "info": "",
    "x": 170,
    "y": 260,
    "wires": []
},
{
    "id": "3102e176923a8fad",
    "type": "comment",
    "z": "133329daaceb6bb3",
    "name": "Check what if both inject clicked",
    "info": "",
    "x": 210,
    "y": 100,
    "wires": []
}
]

You might better to use global.set instead of flow.set
This might help you.

[
{
    "id": "133329daaceb6bb3",
    "type": "tab",
    "label": "flow 3",
    "disabled": false,
    "info": "",
    "env": []
},
{
    "id": "c80bec9a15b703f6",
    "type": "inject",
    "z": "133329daaceb6bb3",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 140,
    "y": 140,
    "wires": [
        [
            "8687551e01cd2045"
        ]
    ]
},
{
    "id": "8687551e01cd2045",
    "type": "function",
    "z": "133329daaceb6bb3",
    "name": "msg_1",
    "func": "global.set(\"msg_1\",\"test\");\n\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 280,
    "y": 140,
    "wires": [
        []
    ]
},
{
    "id": "103b03c39c8d9cae",
    "type": "debug",
    "z": "133329daaceb6bb3",
    "name": "debug 29",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "statusVal": "",
    "statusType": "auto",
    "x": 500,
    "y": 300,
    "wires": []
},
{
    "id": "c043cba9f34ba574",
    "type": "inject",
    "z": "133329daaceb6bb3",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 140,
    "y": 180,
    "wires": [
        [
            "e674c706437cc0f3"
        ]
    ]
},
{
    "id": "e674c706437cc0f3",
    "type": "function",
    "z": "133329daaceb6bb3",
    "name": "msg_2 ",
    "func": "global.set(\"msg_2\",\"hello\");\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 280,
    "y": 180,
    "wires": [
        []
    ]
},
{
    "id": "11f8fb75d4c4e6c0",
    "type": "inject",
    "z": "133329daaceb6bb3",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "1",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 150,
    "y": 300,
    "wires": [
        [
            "1346e34e8c83b2dc"
        ]
    ]
},
{
    "id": "1346e34e8c83b2dc",
    "type": "function",
    "z": "133329daaceb6bb3",
    "name": "check if 2 msg set",
    "func": "var msg1 = global.get(\"msg_1\");\nvar msg2 = global.get(\"msg_2\");\n\nif(msg1 && msg2){\n    msg.payload=\"both message arrived!\";\n}\nelse{\n    msg.payload=\"Nope. Not yet.\";\n}\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 330,
    "y": 300,
    "wires": [
        [
            "103b03c39c8d9cae"
        ]
    ]
},
{
    "id": "62d522a844321ef9",
    "type": "inject",
    "z": "133329daaceb6bb3",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 540,
    "y": 140,
    "wires": [
        [
            "6d109daff443dade"
        ]
    ]
},
{
    "id": "6d109daff443dade",
    "type": "function",
    "z": "133329daaceb6bb3",
    "name": "reset",
    "func": "global.set(\"msg_1\",null);\nglobal.set(\"msg_2\",null);\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 690,
    "y": 140,
    "wires": [
        []
    ]
},
{
    "id": "4e1848c867860d5f",
    "type": "comment",
    "z": "133329daaceb6bb3",
    "name": "Here gonna auto-run",
    "info": "",
    "x": 170,
    "y": 260,
    "wires": []
},
{
    "id": "3102e176923a8fad",
    "type": "comment",
    "z": "133329daaceb6bb3",
    "name": "Check what if both inject clicked",
    "info": "",
    "x": 210,
    "y": 100,
    "wires": []
}
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文