# How to create messages from an array with a variable number of elements

**URL:** <https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991>\
**Category:** General\
**Created:** [2 January 2022 11:55 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991 "2022-01-02T11:55:12Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [2 January 2022 11:55 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/1 "2022-01-02T11:55:12Z")

</div>

Hi, I have a payload which contains Smart meter readings in sub-arrays in the payload.data array. The payload.data array can contain a variable number of sub-arrays, depending on when the last update took place. I am trying to find a way to extract the data in the sub-arrays so I can create separate MQTT messages for each reading to send to my Influx database.  
If I was able to find out how many sub-arrays were in the payload.data array, I could cycle through them to create a message for each. I've searched for a solution and read this link [JavaScript Array length Property (w3schools.com)](https://www.w3schools.com/jsref/jsref_length_array.asp) but I'm unable to work out how to use that information for my purposes.  
On the other hand, there could be a better solution from the one I've been failing to develop. Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [2 January 2022 11:56 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/2 "2022-01-02T11:56:00Z")

</div>

Sorry, meant to include a screenshot of the deebug

 ![Screenshot 2022-01-02 114714](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/151dae731a7aff783887157d6ee4f1379fd156fb.jpeg)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 January 2022 12:52 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/3 "2022-01-02T12:52:58Z")

</div>

Well since you can get the number of elements in the array, you can now use a loop to walk thru each element of the array - REMEMBER the first element of the array is [0] so looking at your debug, the third element in the array is referenced with [2].

Give it a try and if you have an issue show the flow you have created.

Note also, when using a `function` node, you can add `node.warn('x=' + x)` or `node.warn(y)` and those will be displayed in the debug side panel.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 January 2022 13:54 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/4 "2022-01-02T13:54:55Z")

</div>

> [@Bodad](#):
>
> If I was able to find out how many sub-arrays were in the payload.data array, I could cycle through them to create a message for each

No need. Use a change node, move payload.data to payload then pass it through a split node. You will see a message for each array element in the data. Feed that to your database.

You might wish to put a delay node (set to rate limit mode) after the split/before the database node

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [2 January 2022 14:35 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/5 "2022-01-02T14:35:02Z")

</div>

@zenofmud Thanks for the response but the root of the question was that I was unable to find a way to count the number of sub-arrays.

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [2 January 2022 14:37 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/6 "2022-01-02T14:37:25Z")

</div>

@Steve-Mcl I will give your solution a go as it is using built-in nodes and is fairly easy to understand, but for future reference, I'd still like to know how to find the number of elements in an array from within a node red function.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 January 2022 14:43 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/7 "2022-01-02T14:43:48Z")

</div>

> [@Bodad](#):
>
> I will give your solution a go as it is using built-in nodes and is fairly easy to understand

It's more the "node-red way"

> [@Bodad](#):
>
> but for future reference, I'd still like to know how to find the number of elements in an array from within a node red function

It's simply `Array.length`

> **[Array: length - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length)**
>
> The length data property of an Array instance represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

* * *

In a function node...

```JavaScript
const arr = msg.payload.data; // the data array
const len = arr.length;
node.warn(len);
msg.payload = len;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [2 January 2022 15:01 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/8 "2022-01-02T15:01:52Z")

</div>

> [@Steve-Mcl](#):
>
> No need. Use a change node, move payload.data to payload then pass it through a split node. You will see a message for each array element in the data. Feed that to your database.

Thanks for the answer to the length of array question, simple when you know how.  
The split node is asking for a character to split the payload on but I have no idea what settings I would use to split after each object. Can you enlighten me?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 January 2022 15:09 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/9 "2022-01-02T15:09:52Z")

</div>

> [@Bodad](#):
>
> The split node is asking for a character to split the payload on but I have no idea what settings I would use to split after each object. Can you enlighten me?

Since you are splitting an array, concentrate on the **Array** part...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/11ad2dc02d57f999fb80d66b34dfefba39856e65.png)

This is also explained in the built in help...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c58e1378a9876fd93959998f13a362637a96f3d9.png)

In other words - just leave it completely default.

example...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48afd12124e4b3f9eb55834d9c8d7ef4c731e6ec.png)

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [2 January 2022 16:33 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/10 "2022-01-02T16:33:45Z")

</div>

> [@Steve-Mcl](#):
>
> Since you are splitting an array, concentrate on the **Array** part...

Thanks for the pointer. The arrays are of variable length. The first part is a UTC timestamp which is consistently 9 digits but as the second part is a float, it can have one or two digits before the decimal point and from three to thirteen digits after.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 January 2022 16:35 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/11 "2022-01-02T16:35:59Z")

</div>

> [@Bodad](#):
>
> Thanks for the pointer. The arrays are of variable length

The length makes no difference to the split node. If that's what you're asking.

---

<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:** [2 January 2022 16:59 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/12 "2022-01-02T16:59:18Z")

</div>

> [@Bodad](#):
>
> The arrays are of variable length. The first part is a UTC timestamp which is consistently 9 digits but as the second part is a float, it can have one or two digits before the decimal point and from three to thirteen digits after.

The arrays are not of variable length, they are all of length 2. The fact that the number may be displayed with a variable number of digits is irrelevant. The number is still a single element of the array.

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [3 January 2022 11:23 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/13 "2022-01-03T11:23:35Z")

</div>

> [@Colin](#):
>
> The arrays are not of variable length, they are all of length 2. The fact that the number may be displayed with a variable number of digits is irrelevant. The number is still a single element of the array.

Doh!! Just goes to show that it doesn't matter how much you think you understand, there's always room to better understand. I was still thinking in terms of a string rather than elements within an array. Thanks for the help.

---

<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:** [3 January 2022 11:39 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/14 "2022-01-03T11:39:33Z")

</div>

All Number type variables take up the same space in memory in javascript. If you use the number 1.2, for example, it is actually 1.2000000000000 but when printing it the zeros get dropped off.

---

<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:** [4 March 2022 11:40 UTC](https://discourse.nodered.org/t/how-to-create-messages-from-an-array-with-a-variable-number-of-elements/55991/15 "2022-03-04T11:40:26Z")

</div>

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