# Trying to fill msg more efficient but gives errors

**URL:** <https://discourse.nodered.org/t/trying-to-fill-msg-more-efficient-but-gives-errors/66372>\
**Category:** Developing Nodes\
**Created:** [12 August 2022 21:28 UTC](https://discourse.nodered.org/t/trying-to-fill-msg-more-efficient-but-gives-errors/66372 "2022-08-12T21:28:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WesleyFranken](https://avatars.discourse-cdn.com/v4/letter/w/51bf81/32.png) [@WesleyFranken](https://discourse.nodered.org/u/WesleyFranken)\
**Post date:** [12 August 2022 21:28 UTC](https://discourse.nodered.org/t/trying-to-fill-msg-more-efficient-but-gives-errors/66372/1 "2022-08-12T21:28:07Z")

</div>

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

```auto
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?

---

<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:** [12 August 2022 22:03 UTC](https://discourse.nodered.org/t/trying-to-fill-msg-more-efficient-but-gives-errors/66372/2 "2022-08-12T22:03:23Z")

</div>

```auto
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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) or [for ...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)

Or use node.send.

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

return null;

```

---

<div class="post-metadata">

**Author:** ![WesleyFranken](https://avatars.discourse-cdn.com/v4/letter/w/51bf81/32.png) [@WesleyFranken](https://discourse.nodered.org/u/WesleyFranken)\
**Post date:** [12 August 2022 22:23 UTC](https://discourse.nodered.org/t/trying-to-fill-msg-more-efficient-but-gives-errors/66372/3 "2022-08-12T22:23:01Z")

</div>

Thank you, this was exactly what I needed.

I've tested the code with the following adjustment:

```auto
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?

---

<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:** [12 August 2022 22:31 UTC](https://discourse.nodered.org/t/trying-to-fill-msg-more-efficient-but-gives-errors/66372/4 "2022-08-12T22:31:11Z")

</div>

```auto
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

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

return null;

```

---

<div class="post-metadata">

**Author:** ![WesleyFranken](https://avatars.discourse-cdn.com/v4/letter/w/51bf81/32.png) [@WesleyFranken](https://discourse.nodered.org/u/WesleyFranken)\
**Post date:** [12 August 2022 22:43 UTC](https://discourse.nodered.org/t/trying-to-fill-msg-more-efficient-but-gives-errors/66372/5 "2022-08-12T22:43:44Z")

</div>

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"

---

<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:** [26 August 2022 22:44 UTC](https://discourse.nodered.org/t/trying-to-fill-msg-more-efficient-but-gives-errors/66372/6 "2022-08-26T22:44:00Z")

</div>

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