Question : function node

Hi,

it maybe is a dumb question but here goes!

My function node is reciving a message like this :

{
"_msgid":"fd517150.49f97",
"topic":"",
"payload":"Blablabla",
"Appareil":"G102_G008_L308"
} 

On the other side of the function node I want this ["Appareil" proprety split (variable number) + payload] :

Msg #1
{
"_msgid":"fd517150.49f97",
"topic":"",
"payload":"Blablabla",
"Appareil":"G102"
}
Msg #2
{
"_msgid":"fd517150.49f97",
"topic":"",
"payload":"Blablabla",
"Appareil":"G008"
}
Msg #3
{
"_msgid":"fd517150.49f97",
"topic":"",
"payload":"Blablabla",
"Appareil":"L308"
}
Msg #..... and so on

For now i've been able to split the "Appareil" proprety but I can't figure out how to make the payload follow

Inside the function node :

var outputMsgs = [];
var words = msg.Appareil.split("_");
for (var w in words) {
    outputMsgs.push({Appareil:words[w]})
}
return [outputMsgs];

I would use the map() function to turn an array of words into an array of msg objects, with each word inside the new msg -- something like this:

var words = msg.Appareil.split("_");
var outputMsgs = words.map(function(word) {
    return {
        _msgid: msg._msgid,
        topic: msg.topic,
        payload: msg.payload,
        Appareil: word
    };
});
return [outputMsgs];

Essentially, you are copying the incoming msg properties (other than Appareil) into a new msg object. Since you are returning an array of length 1, containing an array of N msgs, the new messages will be sent one at a time out of Port #1 -- which sounds like what you are wanting to do.

Thank you so much :slight_smile: ! It works like a charm!

Sorry for the dumb question but I'm pretty new to this!