# Creating arrays in a function

**URL:** <https://discourse.nodered.org/t/creating-arrays-in-a-function/72212>\
**Category:** General\
**Created:** [14 December 2022 17:08 UTC](https://discourse.nodered.org/t/creating-arrays-in-a-function/72212 "2022-12-14T17:08:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nickdb](https://avatars.discourse-cdn.com/v4/letter/n/e480ec/32.png) [@Nickdb](https://discourse.nodered.org/u/Nickdb)\
**Post date:** [14 December 2022 17:08 UTC](https://discourse.nodered.org/t/creating-arrays-in-a-function/72212/1 "2022-12-14T17:08:34Z")

</div>

Hi  
I’m newish so be gentle.  
I am struggling with a function that creates an array with msg.payload, having been initiated by injecting a random value (doesn’t matter what).

Using the following:

```auto

msg.payload[0] = 1;
msg.payload[1] = 2;
Return msg;

```

(Values or type that you set doesn’t matter)  
The function just passes through the injected msg.  
I can obviously modify msg.payload or insert objects but it just won’t create that array.  
It has worked on other functions but being new, perhaps there is a subtle difference I am missing.

Cheers  
Nick

---

<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:** [14 December 2022 17:14 UTC](https://discourse.nodered.org/t/creating-arrays-in-a-function/72212/2 "2022-12-14T17:14:17Z")

</div>

You inject a number (timestamp) from an inject node I presume.  
So msg.payload is now a number, not an array. So you would need to set it as an array in your function code first  
e.g.  
`msg.payload = [];` initialise an empty array

Then you can do  
`msg.payload[0] = 1;`

> [@Nickdb](#):
>
> having been initiated by injecting a random value (doesn’t matter what).

If you injected a json of value `[]` it would of worked also, as that would have set msg.payload to an empty array.

---

<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:** [14 December 2022 17:22 UTC](https://discourse.nodered.org/t/creating-arrays-in-a-function/72212/3 "2022-12-14T17:22:46Z")

</div>

Or  
`msg.payload = [1,2]`  
`return msg`

---

<div class="post-metadata">

**Author:** ![Nickdb](https://avatars.discourse-cdn.com/v4/letter/n/e480ec/32.png) [@Nickdb](https://discourse.nodered.org/u/Nickdb)\
**Post date:** [14 December 2022 17:57 UTC](https://discourse.nodered.org/t/creating-arrays-in-a-function/72212/4 "2022-12-14T17:57:48Z")

</div>

Thank you both, it is obvious now that you kindly pointed out my error.  
Some rust has been knocked off that part of rhe brain.  
Much appreciated.

dB

---

<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:** [28 December 2022 17:58 UTC](https://discourse.nodered.org/t/creating-arrays-in-a-function/72212/5 "2022-12-28T17:58:09Z")

</div>

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