# Global variable: change specific value inside an array entry

**URL:** <https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 March 2022 05:54 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632 "2022-03-10T05:54:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![na-am](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/na-am/32/7931_2.png) [@na-am](https://discourse.nodered.org/u/na-am)\
**Post date:** [10 March 2022 05:54 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/1 "2022-03-10T05:54:06Z")

</div>

hi@all

i'm filling a global variable from a file to set some switch items in a dashboard  
reading the value is working and sending to the dashboard item is working as well, but how to CHANGE the value in the global variable after the switch was changed in the dashboard?

snip/snap **working** :  
....  
node.send([  
{payload: global.get("assets")[i].switch\_1},  
....

snip/snap **not working** :  
...  
global.set("assets["+i+"]".switch\_1, msg.payload)  
...

i've tried so many different 'syntax styles', none of them were working ☹  
above it's just one of them

thx 4 help

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 March 2022 06:02 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/2 "2022-03-10T06:02:58Z")

</div>

I would like to help, but I can't see the question.

You have shown a working example. Good.

So.. Now what?

---

<div class="post-metadata">

**Author:** ![na-am](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/na-am/32/7931_2.png) [@na-am](https://discourse.nodered.org/u/na-am)\
**Post date:** [10 March 2022 06:07 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/3 "2022-03-10T06:07:23Z")

</div>

unfortunately no, that will create a new global variable:  
"assets[0]" with a value of true  
but i need the existing global variable "assets" changed

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 March 2022 06:12 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/4 "2022-03-10T06:12:23Z")

</div>

Well, all you need to do is (kind of) reverse the command.

Rather than `set` you use the `get`.

So - for instance:

```auto
var x = global.get("assets[0]";

```

Ok, after posting, I don't think that is what you want either.

---

<div class="post-metadata">

**Author:** ![na-am](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/na-am/32/7931_2.png) [@na-am](https://discourse.nodered.org/u/na-am)\
**Post date:** [10 March 2022 06:20 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/5 "2022-03-10T06:20:10Z")

</div>

again, i want to CHANGE the value when it was changed in the dashboard

here is the content of the file:

```auto
[
    {
        "name": "P8",
        "switch_1": false,
        "switch_2": false
    },
    {
        "name": "P9",
        "switch_1": false,
        "switch_2": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 March 2022 06:22 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/6 "2022-03-10T06:22:06Z")

</div>

I can not resolve the meaning of _in the dashboard_.

When you enter a value from the dashboard and want to set the `global` variable to that value?

---

<div class="post-metadata">

**Author:** ![na-am](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/na-am/32/7931_2.png) [@na-am](https://discourse.nodered.org/u/na-am)\
**Post date:** [10 March 2022 06:27 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/7 "2022-03-10T06:27:02Z")

</div>

dashboard is of course irrelevant, it just explains my use case  
i just need the correct SYNTAX to SET the value in a global variable inside an ARRAY of OBJECTS

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 March 2022 06:28 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/8 "2022-03-10T06:28:56Z")

</div>

with progress made, that helps, but don't clear it up to a point I can understand.

> [@na-am](#):
>
> dashboard is of course irrelevant, it just explains my use case

Then please don't include it in the question.  
It only confuses people like me.

---

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [10 March 2022 06:50 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/9 "2022-03-10T06:50:11Z")

</div>

Did you try using the change-node for this task?

> global.set("assets["+i+"]".switch\_1, msg.payload)

I might be wrong, but shouldn´t it be like this:

global.set("assets["+i+"].switch\_1", msg.payload)

---

<div class="post-metadata">

**Author:** ![na-am](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/na-am/32/7931_2.png) [@na-am](https://discourse.nodered.org/u/na-am)\
**Post date:** [10 March 2022 07:17 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/10 "2022-03-10T07:17:40Z")

</div>

> [@nico25](#):
>
> global.set("assets["+i+"].switch\_1", msg.payload)

this creates a new global variable with the name "assets[0].switch\_1"

---

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [10 March 2022 07:29 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/11 "2022-03-10T07:29:11Z")

</div>

Maybe this helps?

```auto
[
    {
        "id": "e5924cbdf8979d8c",
        "type": "inject",
        "z": "f6c346cb44030a48",
        "name": "Change value 1 to 1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "1",
        "payload": "1",
        "payloadType": "str",
        "x": 4250,
        "y": 1540,
        "wires": [
            [
                "0b1582a0c148955c"
            ]
        ]
    },
    {
        "id": "32ab4c4489e5739e",
        "type": "inject",
        "z": "f6c346cb44030a48",
        "name": "change value 1 to 2",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "1",
        "payload": "2",
        "payloadType": "str",
        "x": 4250,
        "y": 1600,
        "wires": [
            [
                "0b1582a0c148955c"
            ]
        ]
    },
    {
        "id": "0b1582a0c148955c",
        "type": "function",
        "z": "f6c346cb44030a48",
        "name": "",
        "func": "if(msg.topic == 1){\nglobal.set(\"aTestVar[\"+0+\"].value1\",msg.payload)\n}\nif(msg.topic == 2){\nglobal.set(\"aTestVar[\"+0+\"].value2\",msg.payload) \n}\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 4560,
        "y": 1620,
        "wires": [
            []
        ]
    },
    {
        "id": "e4f3e83ec7996167",
        "type": "inject",
        "z": "f6c346cb44030a48",
        "name": "Change value 2 to 1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "2",
        "payload": "1",
        "payloadType": "str",
        "x": 4250,
        "y": 1660,
        "wires": [
            [
                "0b1582a0c148955c"
            ]
        ]
    },
    {
        "id": "216df17272664a45",
        "type": "inject",
        "z": "f6c346cb44030a48",
        "name": "change value 2 to 2",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "2",
        "payload": "2",
        "payloadType": "str",
        "x": 4250,
        "y": 1720,
        "wires": [
            [
                "0b1582a0c148955c"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![na-am](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/na-am/32/7931_2.png) [@na-am](https://discourse.nodered.org/u/na-am)\
**Post date:** [10 March 2022 07:54 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/12 "2022-03-10T07:54:17Z")

</div>

Thanks a lot Nico

`global.set("assets["+i+"].switch_1", msg.payload)`  
is working in all Node-red versions i tried now in a docker env  
my "Node-red" is a bit special (cloud based)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 March 2022 09:58 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/13 "2022-03-12T09:58:08Z")

</div>

Generally I suggest trying to avoid syntax like that. As you have found it just causes heartache. Instead I suggest accessing the whole object. It is often more efficient and much simpler

```auto
assets = global.get("assets")
assets[i].switch_1 = msg.payload
global.set("assets",assets)

```

---

<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:** [26 March 2022 09:58 UTC](https://discourse.nodered.org/t/global-variable-change-specific-value-inside-an-array-entry/59632/14 "2022-03-26T09:58:34Z")

</div>

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