# Iteration stops after first loop

**URL:** <https://discourse.nodered.org/t/iteration-stops-after-first-loop/54896>\
**Category:** General\
**Created:** [8 December 2021 21:30 UTC](https://discourse.nodered.org/t/iteration-stops-after-first-loop/54896 "2021-12-08T21:30:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aivaras1](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Aivaras1](https://discourse.nodered.org/u/Aivaras1)\
**Post date:** [8 December 2021 21:30 UTC](https://discourse.nodered.org/t/iteration-stops-after-first-loop/54896/1 "2021-12-08T21:30:49Z")

</div>

I am having some trouble with iteration and for loop. It stops after first loop. Value\_Analog has three objects, first is empty (I don't need it). I need this output  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b8b1dc12e1afd2ba577560fd38d615abd03e8f4.png)

Code in function. I am triggering it by inject node.

```auto
var Value_Analog = global.get('Value_Analog');
var Pasalintasempty = Value_Analog.shift();//pasalinam empty array elementa
let index = 0;

for (let i = 0; i < Value_Analog.length; i++) 
{ 
 msg.payload = ([{[Value_Analog[i].Tag_name]: index}])
   index++;
}
return 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:** [8 December 2021 21:58 UTC](https://discourse.nodered.org/t/iteration-stops-after-first-loop/54896/2 "2021-12-08T21:58:39Z")

</div>

No it iterates over all the array and as it does it overwrites msg.payload each time.  
Try

```auto
msg.payload[i] = {[Value_Analog[i].Tag_name]: i};

```

No need for index as i is doing the same thing.  
[edit] Make sure you define msg.payload as an array before the loop `msg.payload = [];`

---

<div class="post-metadata">

**Author:** ![Aivaras1](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Aivaras1](https://discourse.nodered.org/u/Aivaras1)\
**Post date:** [8 December 2021 22:25 UTC](https://discourse.nodered.org/t/iteration-stops-after-first-loop/54896/3 "2021-12-08T22:25:12Z")

</div>

> [@E1cid](#):
>
> msg.payload = ;

Thank you, it works now.

---

<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:** [22 December 2021 22:26 UTC](https://discourse.nodered.org/t/iteration-stops-after-first-loop/54896/4 "2021-12-22T22:26:03Z")

</div>

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