# Use value of variable as name in push operation

**URL:** <https://discourse.nodered.org/t/use-value-of-variable-as-name-in-push-operation/19263>\
**Category:** General\
**Created:** [17 December 2019 16:01 UTC](https://discourse.nodered.org/t/use-value-of-variable-as-name-in-push-operation/19263 "2019-12-17T16:01:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjxevit](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@bjxevit](https://discourse.nodered.org/u/bjxevit)\
**Post date:** [17 December 2019 16:01 UTC](https://discourse.nodered.org/t/use-value-of-variable-as-name-in-push-operation/19263/1 "2019-12-17T16:01:53Z")

</div>

Is it possible, for the push operation, to use the value of a variable and not the name of the variable, as the attribute name in the object?

Example:  
for (i=1; i\<=5; i++)  
{  
msg.options.push({i:i});  
}

This create the following objects in the msg.options:

msg.options : array[5]  
array[5]  
0: object  
i: 1  
1: object  
i: 2  
2: object  
i: 3  
3: object  
i: 4  
4: object  
i: 5

Here, it always pushes a new attribute "i", with the current value of "i".

But I don't want all objects to be called "i", I want them to be called as the value of the variable "i".

Example:  
msg.options : array[5]  
array[5]  
0: object  
1: 1  
...

Any ideas?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 December 2019 16:10 UTC](https://discourse.nodered.org/t/use-value-of-variable-as-name-in-push-operation/19263/2 "2019-12-17T16:10:57Z")

</div>

Hi @bjxevit

one small request - when sharing code snippets, please use the `</>` icon in the formatting toolbar to ensure they show up as proper code blocks.

You can do it with:

```auto
for (i=1; i<=5; i++) {
   var option = {};
   option[i] = i;
   msg.options.push(option);
}

```

---

<div class="post-metadata">

**Author:** ![bjxevit](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@bjxevit](https://discourse.nodered.org/u/bjxevit)\
**Post date:** [17 December 2019 16:26 UTC](https://discourse.nodered.org/t/use-value-of-variable-as-name-in-push-operation/19263/3 "2019-12-17T16:26:51Z")

</div>

hey @knolleary,

thanks for the quick reply and the short info about sharing code.  
Just started to participate in the forum.

That solution worked for me.
