# Using variables in an array

**URL:** https://discourse.nodered.org/t/using-variables-in-an-array/51692
**Category:** General
**Created:** [30 September 2021 00:17 UTC](https://discourse.nodered.org/t/using-variables-in-an-array/51692 "2021-09-30T00:17:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tfuchs1987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tfuchs1987/32/44508_2.png) [@tfuchs1987](https://discourse.nodered.org/u/tfuchs1987)
#### Post date: [30 September 2021 00:17 UTC](https://discourse.nodered.org/t/using-variables-in-an-array/51692/1 "2021-09-30T00:17:04Z")

</div>

In my function-Node I have to construct the following msg.payload. I am able to use a variable for the id as well for the value of "Fieldname" but I can't figure out how or if I am able to use a variable to describe the path

```auto
msg.payload = [{
    id: iVar,
    fields : { "CAN-THIS-BE-A-VARIABLE-?" : tVar }
}];

```

Thank you for any help in advance!

---

<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: [30 September 2021 00:19 UTC](https://discourse.nodered.org/t/using-variables-in-an-array/51692/2 "2021-09-30T00:19:55Z")

</div>

This is the _best_ I can do to get what I want working.

Just wanting to check I'm not missing any elephants.

```auto
[{"id":"d72327bb.0bc37","type":"string","z":"65c9b63cb09879a0","name":"0","methods":[{"name":"chompLeft","params":[{"type":"str","value":"rgb"}]},{"name":"delLeftMost","params":[{"type":"str","value":","}]},{"name":"delLeftMost","params":[{"type":"str","value":","}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":2810,"y":220,"wires":[["ee603826871cfe95","fcfb52c09951a8be"]]},{"id":"33f88dd62e9a1854","type":"string","z":"65c9b63cb09879a0","name":"","methods":[{"name":"delLeftMost","params":[{"type":"str","value":"rgb,"}]},{"name":"left","params":[{"type":"num","value":"1"}]},{"name":"toInteger","params":[]}],"prop":"payload","propout":"pointer","object":"msg","objectout":"msg","x":2770,"y":160,"wires":[["9e72ba1e6e745ccf","d72327bb.0bc37"]]},{"id":"ee603826871cfe95","type":"function","z":"65c9b63cb09879a0","name":"","func":"let pointer = msg.pointer;\n\n\nflow.set(\"test[\"+pointer+\"]\",msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2980,"y":220,"wires":[[]]},{"id":"fcfb52c09951a8be","type":"debug","z":"65c9b63cb09879a0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2980,"y":260,"wires":[]},{"id":"349b67738dfc8261","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,4,0,0,0","payloadType":"str","x":2610,"y":160,"wires":[["33f88dd62e9a1854"]]},{"id":"0a2b7173df8196ef","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,2,0,0,0","payloadType":"str","x":2610,"y":120,"wires":[["33f88dd62e9a1854"]]},{"id":"9e72ba1e6e745ccf","type":"debug","z":"65c9b63cb09879a0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"pointer","targetType":"msg","statusVal":"payload","statusType":"auto","x":2960,"y":160,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![tfuchs1987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tfuchs1987/32/44508_2.png) [@tfuchs1987](https://discourse.nodered.org/u/tfuchs1987)
#### Post date: [30 September 2021 00:35 UTC](https://discourse.nodered.org/t/using-variables-in-an-array/51692/3 "2021-09-30T00:35:36Z")

</div>

Ok sry to spam the forum I think I got it by trying random stuff xD ... apprently you have to enclose it to brackets [var]

```auto
msg.payload = [{
    id: iVar,
    fields : { [fVar] : tVar }
}];

```

Hope it helps someone

---

<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: [14 October 2021 00:35 UTC](https://discourse.nodered.org/t/using-variables-in-an-array/51692/4 "2021-10-14T00:35:53Z")

</div>

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