Trying to fill msg more efficient but gives errors

Dear community,

I couldn't find the answer so I've to ask it.

I would like to send the following messages. But it gives an error. What am I doing wrong?

The error given at msg+d is "Bad assignment"

The error given at return is "Unrecoverable syntax error".

for(var d=1; c<=65;c++){
    msg+d = { payload: messages[d-1] };
}

return [[msg0,msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11,msg12,msg13,msg14,msg15,msg16,msg17,msg18,msg19,msg20,msg21,msg22,msg23,msg24,msg25,msg26,msg27,msg28,msg29,msg30,msg31,msg32,msg33,msg34,msg35,msg36,msg37,msg38,msg39,msg40,msg41,msg42,msg43,msg44,msg45,msg46,msg47,msg48,msg49,msg50,msg51,msg52,msg53,msg54,msg55,msg56,msg57,msg58,msg59,msg60,msg61,msg62,msg63,msg64,msg65,msg66]];

Is there another way other defining every msg?

const output = [];
for(let c=0; c<65; c++){
    output.push( { payload: messages[c] });
}

return [output];

I am presuming messages is an array set earlier in your code.

You could also use map() or for ...of

Or use node.send.

for(let c=0; c<65; c++){
    node.send( { payload: messages[c] });
}

return null;
1 Like

Thank you, this was exactly what I needed.

I've tested the code with the following adjustment:

var messages = [];
const output = [];

for(var d=0; d<=64;d++){
    messages[d] = 0;
    output.push( { payload: messages[d] });
}

return [output];

Again, thank you very much.

Maybe a bit off topic, where should I insert null if I do not want to send the payload?

const output = [];
let msg1 = {}
for(let c=0; c<65; c++){
   if(c===2){
      msg1 = null;
   }else{
      msg1 = { payload: messages[c] };
   }
    output.push( msg1 );
}

return [output];

Or with node.send

for(let c=0; c<65; c++){   
   if(c != 2){
      node.send( { payload: messages[c] });
   }
}

return null;
1 Like

I was too quick with asking, I've had the answer already with "your" .push operator. It works like a charm!

It's way past midnight for me, so maybe with some sleep I will not ask more resources from this forum than necessary.

My final "Thank you very much"

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