# Reference variable name in a loop

**URL:** <https://discourse.nodered.org/t/reference-variable-name-in-a-loop/35556>\
**Category:** General\
**Created:** [8 November 2020 17:02 UTC](https://discourse.nodered.org/t/reference-variable-name-in-a-loop/35556 "2020-11-08T17:02:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)\
**Post date:** [8 November 2020 17:02 UTC](https://discourse.nodered.org/t/reference-variable-name-in-a-loop/35556/1 "2020-11-08T17:02:20Z")

</div>

Good evening

I have a function looking at a string of 6 characters, my function uses a loop to look at each character in turn and if the value matches a temp value a event occurs.

I have at the beginning of the function

var temp1 = some value  
var temp2 = some value

etc

if my logic is true then I want to do the following where x is the array position (1-6)

temp + x = context.get('T1'); this fails

if its static

temp1 = context.get('T1'); it works

Many thanks  
Andrew

---

<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:** [8 November 2020 17:47 UTC](https://discourse.nodered.org/t/reference-variable-name-in-a-loop/35556/2 "2020-11-08T17:47:34Z")

</div>

> [@fogmajor](#):
>
> temp + x = context.get('T1');

What is temp?

It would probably be best to show us your function as it isn't at all clear what you are trying to do. Copy/paste the code here and use the `</>` button at the top of the forum entry window to paste it in.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 November 2020 17:55 UTC](https://discourse.nodered.org/t/reference-variable-name-in-a-loop/35556/3 "2020-11-08T17:55:01Z")

</div>

> [@fogmajor](#):
>
> temp + x = context.get('T1'); this fails

Not sure what you are trying to achieve there? That isn't valid JavaScript.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 November 2020 18:22 UTC](https://discourse.nodered.org/t/reference-variable-name-in-a-loop/35556/4 "2020-11-08T18:22:08Z")

</div>

You could set an array entry for each temp.  
`temp[x] = context.get('T1');`  
assuming x is a number that increments.  
Best to declare temp earlier in your code.  
`let temp = [];`  
access `temp[0]` etc

---

<div class="post-metadata">

**Author:** ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)\
**Post date:** [9 November 2020 15:10 UTC](https://discourse.nodered.org/t/reference-variable-name-in-a-loop/35556/5 "2020-11-09T15:10:23Z")

</div>

Hi  
Many thanks for that it is just what I was looking for, so glad you understood my convoluted question.

Regards  
Andrew

---

<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:** [8 January 2021 15:10 UTC](https://discourse.nodered.org/t/reference-variable-name-in-a-loop/35556/6 "2021-01-08T15:10:23Z")

</div>

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