# Declaring array of objects in payload

**URL:** <https://discourse.nodered.org/t/declaring-array-of-objects-in-payload/77657>\
**Category:** General\
**Tags:** javascript\
**Created:** [17 April 2023 12:04 UTC](https://discourse.nodered.org/t/declaring-array-of-objects-in-payload/77657 "2023-04-17T12:04:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JesFo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jesfo/32/50735_2.png) [@JesFo](https://discourse.nodered.org/u/JesFo)\
**Post date:** [17 April 2023 12:05 UTC](https://discourse.nodered.org/t/declaring-array-of-objects-in-payload/77657/1 "2023-04-17T12:05:00Z")

</div>

Hello. I'm trying to declare an array of objects into my **msg.payload** using a JavaScript node.  
I declare my array as such:

```auto
        msg.payload = {
            DeviceID : '0',
            Sensors : []
        }

```

And the **Sensors** array is suposed to contain objects that look like this:

```auto
{
     SensorID : 'NULL',
     Value : 0
}

```

However, I'm having trouble inserting a new instance of an object into **Sensors**. For example: If I try to insert a value after the array declaration, ex: **msg.payload.Sensors[index].Value = myValue**, I get an error telling me that the array **Sensors** is undeclared, and that I can't set it's value.  
How would i go about declaring the Sensors array properly in order to insert new values at a given index?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 April 2023 12:21 UTC](https://discourse.nodered.org/t/declaring-array-of-objects-in-payload/77657/2 "2023-04-17T12:21:59Z")

</div>

I don't think you can assign values to payload.Sensors[n] until that array element exists.  
You need to push the object onto the array.

```auto
msg.payload = {
    DeviceID: '0',
    Sensors: []
}
const obj = {"SensorID": 42, "Value": 98}
msg.payload.Sensors.push(obj)

```

---

<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:** [17 April 2023 12:28 UTC](https://discourse.nodered.org/t/declaring-array-of-objects-in-payload/77657/3 "2023-04-17T12:28:49Z")

</div>

Or you would have to declare the Sensors index as an object first.  
e.g.

```auto
msg.payload.Sensors[index] = {}; // make sure outside loop or will over write index value.
msg.payload.Sensors[index].value = 0;

```

Or

```auto
msg.payload.Sensors[index] = {SensorID: null, Value: 0};

```

---

<div class="post-metadata">

**Author:** ![JesFo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jesfo/32/50735_2.png) [@JesFo](https://discourse.nodered.org/u/JesFo)\
**Post date:** [18 April 2023 05:24 UTC](https://discourse.nodered.org/t/declaring-array-of-objects-in-payload/77657/4 "2023-04-18T05:24:21Z")

</div>

Thank you. This worked for me.

---

<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:** [2 May 2023 05:25 UTC](https://discourse.nodered.org/t/declaring-array-of-objects-in-payload/77657/5 "2023-05-02T05:25:17Z")

</div>

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