# 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:** 5\
**Page:** 1

<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:** [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/1 "2023-01-19T18:11:57Z")

</div>

Hi!  
Apologies in advance, this may sound a simple question.

I would like to now how to create an element of msg.payload (or better, global.payload) based on the value of msg.payload.

In other words: I am receiving a msg.payload which is a number: "1". I want to store the state "True" in global.payload.1.

I can do it by hand, but I thought it could be more elegant if it was a function, but javascript is a spice I still don't master well.

My wrong flow (super basic)  
[flows.json](https://discourse.nodered.org/uploads/short-url/zUUPKbkCpLniX3cMrpXniyiKpn8.json) (814 Bytes)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [19 January 2023 18:37 UTC](https://discourse.nodered.org/t/add-an-element-on-the-go-based-on-the-value-of-msg-payload/73930/2 "2023-01-19T18:37:02Z")

</div>

I would highly recommend to go through the documentation and how to [work with messages](https://nodered.org/docs/user-guide/messages). The videos in that document should help.

---

<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)

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [20 January 2023 19:33 UTC](https://discourse.nodered.org/t/add-an-element-on-the-go-based-on-the-value-of-msg-payload/73930/4 "2023-01-20T19:33:56Z")

</div>

> [@vongomben](#):
>
> I end up creating a msg.payload.3 set to false.

There are easier ways to achieve it, but yes ... this works.  
Have you tried

```auto
msg.payload[num] = false;

```

> [@vongomben](#):
>
> `global.meridiana[msg.payload.Element1] = false;`

Did you read the [documentation](https://nodered.org/docs/user-guide/context) regarding context storage? If not - I'd like to strongly recommend to do it.

> [@vongomben](#):
>
> Why I have msg.payload assigned to 3 and not payload.3 = false from this function here?

```auto
let num = msg.payload;
    console.log(typeof(msg.payload)); // check typeof msg.payload
msg.payload[num] = false;
    console.log(typeof(msg.payload)); // did you change it to another type?
return msg;

```

That's Javascript! I agree, it could be nicer & throw an error...

---

<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:** [21 March 2023 19:34 UTC](https://discourse.nodered.org/t/add-an-element-on-the-go-based-on-the-value-of-msg-payload/73930/5 "2023-03-21T19:34:07Z")

</div>

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