Function: convert Messages to array

Hello guys,
I want to create as output from a function an array from incoming datas like this example; example "Phone".
My code, modified from this example:

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!

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?

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].

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

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

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

then at the end

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.

@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!!!

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:

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!

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

Perfect, thank you for your answer! :wink:
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!

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.

@Colin
Thanks for your detailed answer!