# Add an element on the go based on the value of msg.payload

**URL:** <https://discourse.nodered.org/t/add-an-element-on-the-go-based-on-the-value-of-msg-payload/73930>\
**Category:** General\
**Tags:** javascript\
**Created:** [19 January 2023 18:11 UTC](https://discourse.nodered.org/t/add-an-element-on-the-go-based-on-the-value-of-msg-payload/73930 "2023-01-19T18:11:57Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![vongomben](https://avatars.discourse-cdn.com/v4/letter/v/a4c791/32.png) [@vongomben](https://discourse.nodered.org/u/vongomben)\
**Post date:** [20 January 2023 18:15 UTC](https://discourse.nodered.org/t/add-an-element-on-the-go-based-on-the-value-of-msg-payload/73930/3 "2023-01-20T18:15:46Z")

</div>

Thanks.  
I've seen, reviewed and looked more into the topic, and partially succeded in what I want to do.

if I send the number 3 to this function (takjen from [this helpful](https://discourse.nodered.org/t/how-to-add-element-to-msg-payload/3361) post)

```auto
let num = msg.payload;

msg.payload = {
    Element1: num,
    Element2: "Two",
    Element3: "Three"
}
msg.payload[msg.payload.Element1] = false;

return msg;

```

I end up creating a msg.payload.3 set to false.

Sadly I ended up stopping in the next problem: since I want to store this values in a global object, I've previously created three global variables that I want to update.

I create them using a change node, that I inject before testing the function:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d949b568709e681e54254ce959b169071d74df1.png)

Then I edited my code in the function: I want to update one of those variables based on the number passing throught the function:

```auto
let num = msg.payload;

msg.payload = {
    Element1: num,
    Element2: "Two",
    Element3: "Three"
}
msg.payload[msg.payload.Element1] = false;

global.meridiana[msg.payload.Element1] = false;

return msg;

```

**This gives me an error** : `"TypeError: Cannot set property '3' of undefined"`

Why is `global.meridiana["3"]` undefined since I previously declared it in the change node?

* * *

Why I have msg.payload assigned to 3 and not payload.3 = false from this function here?

```auto
let num = msg.payload;

msg.payload[num] = false;

return msg;

```

Sorry for the many questions: hoping in some help

Thanks in advance.  
[flows (1).json](https://discourse.nodered.org/uploads/short-url/xtbYWmQIW91j9X0Go0VP8pwmuP8.json) (1.9 KB)

---

_[View the full topic](https://discourse.nodered.org/t/add-an-element-on-the-go-based-on-the-value-of-msg-payload/73930)._
