# Add to array in an object

**URL:** <https://discourse.nodered.org/t/add-to-array-in-an-object/93296>\
**Category:** General\
**Created:** [20 November 2024 16:34 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296 "2024-11-20T16:34:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [20 November 2024 16:34 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/1 "2024-11-20T16:34:08Z")

</div>

I've got an object msg.payload[0] and I now want to add another element to the array i.e I want to create msg.payload[1] as well

I can't get how to do this into my head

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe24e6effc114ed92a921a7901b03def65563705.png)

I'm doing this in Blockly but if someone could show me what to do in JavaScript I'll hopefully be able to transcode it 🙂

---

<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:** [20 November 2024 16:36 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/2 "2024-11-20T16:36:22Z")

</div>

In javascript you could Push an element.

```auto
msg.payload.push({"prop":1})

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [20 November 2024 16:39 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/3 "2024-11-20T16:39:10Z")

</div>

oh dear - don't think I've got that in Blockly ☹

I'm thinking that maybe I get the [0] value - save it temp - then delete obj - recreate it with 2 elements and repopulate [0]

---

<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:** [20 November 2024 16:40 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/4 "2024-11-20T16:40:25Z")

</div>

Or

```auto
let index = msg.payload.count
msg.payload[index] = {"prop":1}

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [20 November 2024 16:42 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/5 "2024-11-20T16:42:23Z")

</div>

I'll try that  
Unfortunately got bigger problems - my Blockly code has just vanished on me! ☹

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c5f361c35e50ba45e0cee50d8e6206bc114d59ac.png)  
But flows.json.backup has saved me 🙂  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42d94572c7d70e6695929e4bc9104cba3c226996.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [20 November 2024 17:02 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/6 "2024-11-20T17:02:33Z")

</div>

Worked out what to do  
Just create an empty list  
then add first Object to it  
then add second Object to it  
then set msg.payload to the list

Dead simple 🙂

PS Blocky has a insert at end of list block which is converted to a push 🙂  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/76fbc91554c3b0f9699618eae067c21fe368680f.png)

---

<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:** [20 November 2024 19:47 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/7 "2024-11-20T19:47:44Z")

</div>

> [@cymplecy](#):
>
> then add first Object to it  
> then add second Object to it

That is what `push` does 😄

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [20 November 2024 20:38 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/8 "2024-11-20T20:38:02Z")

</div>

I'ts been 6 months since I last touched JS 🙂

---

<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:** [18 February 2025 20:38 UTC](https://discourse.nodered.org/t/add-to-array-in-an-object/93296/9 "2025-02-18T20:38:26Z")

</div>

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