# Function: convert Messages to array

**URL:** <https://discourse.nodered.org/t/function-convert-messages-to-array/18872>\
**Category:** General\
**Created:** [6 December 2019 17:21 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872 "2019-12-06T17:21:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [6 December 2019 17:21 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/1 "2019-12-06T17:21:58Z")

</div>

Hello guys,  
I want to create as output from a function an array from incoming datas like [this](https://nodered.org/docs/user-guide/images/messages_debug.png) example; example "Phone".  
My code, modified from [this](http://www.steves-internet-guide.com/node-red-functions/) example:

```auto
var m_out = [];
for (int i=0; i<=3; i++) {
var newmsg = {payload:msg.payload.abc[i].date, topic:"test"}
m_out.push(newmsg);
}
return [m_out];

```

How can I create the output as array and how can I access to the array? I dont' see in the debug an array but only the different output messages :-/  
What I do wrong?  
Thanks a lot!

---

<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:** [6 December 2019 17:45 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/2 "2019-12-06T17:45:06Z")

</div>

Not sure exactly what you mean. Do you mean you want to pass on a message with an array of values in msg.payload for access by later nodes?

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [6 December 2019 17:56 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/3 "2019-12-06T17:56:05Z")

</div>

Thank you for the fast answer @Colin  
Sorry for by bad english.  
Oh sorry. I try to explain it more detailed:  
I get datas from a http request (format json) and can access with this command: msg.payload.abc[i].date  
Now, I have to convert the datas in a oder data/time format (i have just the function) and so I want to put out the modifed datas in an array format where I can access on the single data array like msg.payload.xyz[i].

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [6 December 2019 17:58 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/4 "2019-12-06T17:58:32Z")

</div>

If you "just" need to play with elements in an array then the javascript .map function is probably your best bet.

---

<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:** [6 December 2019 18:00 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/5 "2019-12-06T18:00:14Z")

</div>

In your function, instead of creating an array of messages create an array of values, so something like

```auto
m_out.push(msg.payload.abc[i].date)

```

then at the end

```auto
msg.payload = m_out
return msg

```

Then in the next node you can reference `msg.payload[i]`

As @dceejay suggests you could use the javascript map function instead of a loop in the function.

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [6 December 2019 18:31 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/6 "2019-12-06T18:31:08Z")

</div>

@Colin  
Thanks a lot for your inputs and the code example. It works!!!

@colin @dceejay  
Thanks for your inputs.  
I will search info about the javascript map function.

Thanks!!!

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [6 December 2019 19:43 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/7 "2019-12-06T19:43:51Z")

</div>

Now I try to use the javascript map function.  
I'm a beginner with javascript a so I have some problems...  
How the function know, which function the must run for begin and which function is a subfunction.  
My code, where I hope that it works:

```auto
function outputdatetime () {
    const array1= [];
    
    for (i = 0; i <=3; i++) {
        array1[i] = msg.payload.dayForecasts[i].date;
        const map1 = array1.map(convertdatetime);
        }
msg.payload = map1;        
return msg;
}

function convertdatetime (datetime) {
    var d = new Date(datetime);
    var year = d.getFullYear();
    var month = d.getMonth()+1;
    var day = d.getDate();
    
    if(day.toString().length==1){
        day = '0' + day;
    }
    
    if(month.toString().length==1){
        month = '0' + month;
    }
    
   return (day + '.' + month + '.' + year);
}

```

Thanks for your support!

---

<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:** [7 December 2019 10:03 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/8 "2019-12-07T10:03:53Z")

</div>

You don't need the loop when you use the map function, it transforms the whole array. Look at the examples at the end of this

> **[Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)**
>
> The map() method creates a new array with the results of calling a provided function on every element in the calling array.

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [9 December 2019 19:14 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/9 "2019-12-09T19:14:46Z")

</div>

Perfect, thank you for your answer! 😉  
A general question:  
In javascript how a function know, which function must run for begin and which function is a subfunction? For example i know in C/C++ it is void main().  
Someone know where can I find on the web good infos for learning javascript?  
Thanks!

---

<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:** [9 December 2019 20:27 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/10 "2019-12-09T20:27:45Z")

</div>

Assuming you are talking about js in a Function node then there is no equivalent of main(). The code in the node will do nothing until it receives a message, at which time execution starts at the beginning of the Function node code. Obviously any functions defined there are skipped over to find the entry point.

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [11 December 2019 17:26 UTC](https://discourse.nodered.org/t/function-convert-messages-to-array/18872/11 "2019-12-11T17:26:07Z")

</div>

@Colin  
Thanks for your detailed answer!
