# Adding a dynamic amount of values to a JavaScript Object Property

**URL:** <https://discourse.nodered.org/t/adding-a-dynamic-amount-of-values-to-a-javascript-object-property/54806>\
**Category:** General\
**Created:** [7 December 2021 04:21 UTC](https://discourse.nodered.org/t/adding-a-dynamic-amount-of-values-to-a-javascript-object-property/54806 "2021-12-07T04:21:27Z")\
**Posts on this page:** 6\
**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:** [7 December 2021 04:21 UTC](https://discourse.nodered.org/t/adding-a-dynamic-amount-of-values-to-a-javascript-object-property/54806/1 "2021-12-07T04:21:28Z")

</div>

Hi Guys,

I am desperatly trying to add values to an object property. The API I want to communicate with needs all information delivered that way and the amount of values vary.

```auto
msg.object = {property: {[var1] : "value1",[var2] : "value2",...}};

```

I tried some functions like .push() and splice() but they seem not to work for this use case

Thank you so much in advance for any help ! 🙏

---

<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:** [7 December 2021 04:38 UTC](https://discourse.nodered.org/t/adding-a-dynamic-amount-of-values-to-a-javascript-object-property/54806/2 "2021-12-07T04:38:16Z")

</div>

Can you give some more info, ie. what is the input and what should become the output ?

---

<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:** [7 December 2021 04:47 UTC](https://discourse.nodered.org/t/adding-a-dynamic-amount-of-values-to-a-javascript-object-property/54806/3 "2021-12-07T04:47:55Z")

</div>

I have this object

```auto
msg.object = [{property: {[var1] : "value1"}}]

```

and I want to add more values like with an array I use the .push()-function

```auto
msg.object = {property: {[var1] : "value1",[var2] : "value2",...}};

```

I could write this manually but I have to dynamically add more values because the amount is uncertain

thanks for the reply!

---

<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:** [7 December 2021 05:04 UTC](https://discourse.nodered.org/t/adding-a-dynamic-amount-of-values-to-a-javascript-object-property/54806/4 "2021-12-07T05:04:18Z")

</div>

`msg.object` is setup as an array, but only with a single object. There is no `push` method for objects, you will need to create new properties:

```auto
let obj = [{ property: { key_1: "value1" } }]

let key2 = "key_2"
let key2_value = "value2"

let setObj = obj[0].property // first get the property key from the single array and create new keys from there. 

setObj[key2] = key2_value

// or as a short hand you could do: 
// obj[0].property[key2] = key2_value

msg.object = obj

return msg;

```

Output:

```auto
[{"property":{"key_1":"value1","key_2":"value2"}}]

```

---

<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:** [7 December 2021 18:10 UTC](https://discourse.nodered.org/t/adding-a-dynamic-amount-of-values-to-a-javascript-object-property/54806/5 "2021-12-07T18:10:16Z")

</div>

`bakman2` I cannot thank you enough! Your solution works like a charm! To generate an unknown amount of property keys I just had to adjust a little bit of your code to let it act like the `push` method

```auto
msg.object = [{ property: { }}];

for (let i = 0 ; i<5; i++){
msg.object[0].property["key_" +i] = "value_"+i;
}

return msg;

```

![66351358f3618512](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/2/02e35519ff475d88a9e60f0568621554753f2a7f.png)

I am so glad the node-red community is that awesome 🙌

---

<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 December 2021 18:10 UTC](https://discourse.nodered.org/t/adding-a-dynamic-amount-of-values-to-a-javascript-object-property/54806/6 "2021-12-21T18:10:29Z")

</div>

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