# If statement in for loop

**URL:** <https://discourse.nodered.org/t/if-statement-in-for-loop/65052>\
**Category:** General\
**Tags:** http-request\
**Created:** [13 July 2022 13:24 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052 "2022-07-13T13:24:32Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 July 2022 08:53 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/21 "2022-07-15T08:53:40Z")

</div>

> [@zenofmud](#):
>
> in the loop you need to store the msg’s you want in an array. Then when the loop finishes move that array to msg.payload and send the message.

I've told you what to try so try it. The best way to learn is by doing and discovering your own mistakes and correcting them. I am happy to `guide` you in solving `your` problem, but I'm not going to do the work for you. So try something and then if you have a problem, show your code (opy and paste your code do not post images of it)

---

<div class="post-metadata">

**Author:** ![cyntia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cyntia/32/64546_2.png) [@cyntia](https://discourse.nodered.org/u/cyntia)\
**Post date:** [15 July 2022 10:28 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/22 "2022-07-15T10:28:29Z")

</div>

I have it as undefined. I made so many research.I'm quite new in the domain and all what I have written now ,I searched and tested it these days.Now I'm really blocked after trying to get my payload as an array.  
Here is the code and the output.

```auto
 msg.result = [];
 for(var i =0;i<msg.payload.length; i++){
    msg.payload[i].Zeit=msg.payload[i].Zeit.substr(6, 4)+"."+msg.payload[i].Zeit.substr(3, 2)
    +"."+msg.payload[i].Zeit.substr(0, 2)+"."+msg.payload[i].Zeit.substr(11,2)
    +":"+msg.payload[i].Zeit.substr(14,2)+":"+msg.payload[i].Zeit.substr(15);
    
    msg.payload[i].Zeit = new Date(msg.payload[i].Zeit).toISOString();
    msg.payload[i].Zeit = new Date(msg.payload[i].Zeit);
    time=new Date();
    time.setHours(time.getHours() -4);
    time.toISOString("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    if(msg.payload[i].Zeit.getTime()<time.getTime()){
         { continue; }
    }
   msg.result[i]=msg.payload[i];
   node.send(msg.result);
   msg.payload= msg.result;
 }
 return msg;

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4e41de8d4917c0891c0f5d25b551a9bf177c690.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 July 2022 10:41 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/23 "2022-07-15T10:41:11Z")

</div>

As I've said before

> [@zenofmud](#):
>
> in the loop you need to store the msg’s you want in an array. `Then when the loop finishes` move that array to msg.payload and send the message.
> 
> As it is, you are sending a msg during each iteration of the loop if the condition is met.

Do you know where in your code the `For` loop ends?

---

<div class="post-metadata">

**Author:** ![cyntia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cyntia/32/64546_2.png) [@cyntia](https://discourse.nodered.org/u/cyntia)\
**Post date:** [15 July 2022 10:53 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/24 "2022-07-15T10:53:07Z")

</div>

yes.just before the return msg.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 July 2022 10:55 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/25 "2022-07-15T10:55:54Z")

</div>

Then why haven't you moved the code that moves the array to the msg out of the loop as I have suggested multiple times?

---

<div class="post-metadata">

**Author:** ![cyntia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cyntia/32/64546_2.png) [@cyntia](https://discourse.nodered.org/u/cyntia)\
**Post date:** [15 July 2022 11:02 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/26 "2022-07-15T11:02:41Z")

</div>

I did @zenofmud but the element from the if are not passed ,rather all the payload.let me show you please

---

<div class="post-metadata">

**Author:** ![cyntia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cyntia/32/64546_2.png) [@cyntia](https://discourse.nodered.org/u/cyntia)\
**Post date:** [15 July 2022 11:08 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/27 "2022-07-15T11:08:03Z")

</div>

```nohighlight

 msg.result = [];
 for(var i =0;i<msg.payload.length; i++){
     
    msg.payload[i].Zeit=msg.payload[i].Zeit.substr(6, 4)+"."+msg.payload[i].Zeit.substr(3, 2)
    +"."+msg.payload[i].Zeit.substr(0, 2)+"."+msg.payload[i].Zeit.substr(11,2)
    +":"+msg.payload[i].Zeit.substr(14,2)+":"+msg.payload[i].Zeit.substr(15);
    
    msg.payload[i].Zeit = new Date(msg.payload[i].Zeit).toISOString();
    msg.payload[i].Zeit = new Date(msg.payload[i].Zeit);
    time=new Date();
    time.setHours(time.getHours() -4);
    time.toISOString("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    
    if(msg.payload[i].Zeit.getTime()<time.getTime()){
         { continue; }
    }
   msg.result[i]=msg.payload[i];
   }
   msg.payload=msg.result;
   
 
 return msg;

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 July 2022 11:56 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/28 "2022-07-15T11:56:12Z")

</div>

Show me what comes out.

---

<div class="post-metadata">

**Author:** ![cyntia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cyntia/32/64546_2.png) [@cyntia](https://discourse.nodered.org/u/cyntia)\
**Post date:** [15 July 2022 12:23 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/29 "2022-07-15T12:23:14Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20d86db54a5ce7bcdc9a01e8202a23a64c481846.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18852fa74f8d0846830ccb369da67a6e029aa18c.png)

---

<div class="post-metadata">

**Author:** ![cyntia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cyntia/32/64546_2.png) [@cyntia](https://discourse.nodered.org/u/cyntia)\
**Post date:** [15 July 2022 12:24 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/30 "2022-07-15T12:24:14Z")

</div>

first it gives out the total number of objects in payload as before.Second it is undefined

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 July 2022 12:31 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/31 "2022-07-15T12:31:28Z")

</div>

Try this

```auto
// get current ISO time as string
    time=new Date();
    time.setHours(time.getHours() -4);
    time.toISOString("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

var tem={}; // create a tempeory new holder for messages
    tem.payload=[]; // define the payload as an array
let ti = 0; // need this to index tem.payload

// loop thru msg.payload array and reformat the 'Zeit' date 
for(var i = 0; i < msg.payload.length; i++){
    
    let loop_date = msg.payload[i].Zeit
    let date_arr = loop_date.split(" ") // split the date/time to an array
    let date_year = date_arr[0].split(".") // split the date to an array
    let date_time = date_arr[1].split(":") // split the time to an array
    // rebuild the date/time into needed format
    loop_date = date_year[2]+"."+
                date_year[1]+"."+
                date_year[0]+"."+
                date_time[0]+":"+
                date_time[1]+":"+
                date_time[2];
               
    // convert to string and put back in msg
    msg.payload[i].Zeit = new Date(loop_date);
    
    if(msg.payload[i].Zeit.getTime() < time.getTime()){
         continue; 
    }

    
    tem.payload[ti] = msg.payload[i]; // move the payload to tem.payload
    ti++ // incriment the index for tem.payload
    //this is the end of the FOR loop
 }
 
msg = tem //msg = tem
return msg;

```

---

<div class="post-metadata">

**Author:** ![cyntia](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cyntia/32/64546_2.png) [@cyntia](https://discourse.nodered.org/u/cyntia)\
**Post date:** [15 July 2022 14:01 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/32 "2022-07-15T14:01:59Z")

</div>

Thank you so much it has worked pretty good.And I understood what you did.

---

<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:** [29 July 2022 14:02 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/33 "2022-07-29T14:02:15Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/if-statement-in-for-loop/65052.md?page=1)
