# 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:** 20
**Page:** 1

<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: [13 July 2022 13:24 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/1 "2022-07-13T13:24:32Z")

</div>

I want to retrieve all objects which are true(if condition).but I have only one output(the first true).  
I need to have it in an array in order to be able to send it in an http respond node.

could someone please help?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/11ed0a9010f0173a85748858b6f80d19e813de23.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: [13 July 2022 13:52 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/2 "2022-07-13T13:52:30Z")

</div>

do a google search on 'javascript how to break out of a for loop'

---

<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: [13 July 2022 14:16 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/3 "2022-07-13T14:16:05Z")

</div>

Thank you.yes i checked and corrected it but the other problem is receiving all the other objects in an array.it returns only one(the first object)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/366fc9f9a5680a03cfd93fa86a3b8f8c52774364.png)

---

<div class="post-metadata">

### Author: ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)
#### Post date: [13 July 2022 14:29 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/4 "2022-07-13T14:29:49Z")

</div>

On line 10 you are setting the value of `msg.payload` to the value of `msg.payload[i]` over and over each time the loop runs, overwriting the previous value. Then when you return the `msg` on line 12 it will send only the last value set on line 10 for `msg.payload`.

---

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

</div>

thanks you @HaroldPetersInskipp @zenofmud .Now I have it. but will like to know if i can receive them in one block i.e an array.because in this manner I can't send them in my http respond note,or it doesn't work.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/958fee917488a962ca5f59df2ecc989934873496.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: [13 July 2022 15:28 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/6 "2022-07-13T15:28:43Z")

</div>

Could you copy and paste your code. Also indenting your code makes it much easier to read and may benefiy you in the future (like a year from now when you come back and look at it and say "What the heck was I doing here???". I can say this from experience, add some documentation to your code)

---

<div class="post-metadata">

### Author: ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)
#### Post date: [13 July 2022 15:40 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/7 "2022-07-13T15:40:00Z")

</div>

Rather than change the value of msg.payload and return it. Try creating a new array and returning it.

You could add something like this to your code:

```auto
// declare msg.result to be an empty array, (I put declarations near the top of my code)
msg.result = [];

msg.result[i] = msg.payload[i]

```

---

<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: [13 July 2022 16:36 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/8 "2022-07-13T16:36:00Z")

</div>

here is it @zenofmud .Tahnks for the advice.

```auto
 var tem={};
 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; }
        
    }
    tem.payload=msg.payload[i];
    node.send(tem);
     
 }return;

```

---

<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: [13 July 2022 19:14 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/9 "2022-07-13T19:14:35Z")

</div>

Can you show us a msg.payload going into the function node looks like? Put a `debug` node on the output of the node feeding into the `function` node.

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<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: [13 July 2022 19:56 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/10 "2022-07-13T19:56:52Z")

</div>

here is it ,and it is how I want it to be after the function node,

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ada589070e717f7450402546dd4ca2a747157b7d.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: [13 July 2022 21:36 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/11 "2022-07-13T21:36:08Z")

</div>

You haven't expanded the objects so I can see the actual data and an image of the data is useless becaue I'm not going to type it all in to test your issue.

So please look at the way you can copy the data and then insert it into a reply. Thanks

---

<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: [14 July 2022 00:04 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/12 "2022-07-14T00:04:07Z")

</div>

@zenofmud these are how each object ist

---

<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: [14 July 2022 00:28 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/13 "2022-07-14T00:28:55Z")

</div>

![array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c30c570d26c34556353c60e95fa2559c0973585e.jpeg)

You see that triangle? If you click on it it will open up and show your array items. There will be a triangle next to each entry, clicl on one and it will open up to show that array occurrences content. Then click on the `Copy Data' option (see the GIF in one of my prior posts) and post the results in a reply.

---

<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: [14 July 2022 04:14 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/14 "2022-07-14T04:14:44Z")

</div>

{  
"Zeit":"11.07.2022 13:50:00",  
"Windrichtung":"325,0",  
"Windgeschwindigkeit":"1,1",  
"Lufttemperatur":"18,3",  
"Temperatur5cm":"19,7",  
"relFeuchte":"81,1",  
"Luftdruck":"1001,5",  
"Windchill":"19,35",  
"Niederschlag":"0,0",  
"Taupunkt":"15,0"  
}

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2ffcbbc234f4a528bf04cc2c147ab86a4a90636d.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: [14 July 2022 09:17 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/15 "2022-07-14T09:17:55Z")

</div>

Ahh, so you have a date/time in the format of a string and you want to reformat it and compare it against the current date/time. Saying that in the begining would have been helpful.

Try this as your function node. I've rewritten it and added some comments

```auto
[{"id":"55654507e5faba3d","type":"function","z":"09779a027f808016","name":"","func":"// get current ISO time as string\n time=new Date();\n time.setHours(time.getHours() -4);\n time.toISOString(\"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'\");\n\nvar tem={};\n\n// loop thru msg.payload array and reformat the 'Zeit' date \nfor(var i = 0; i < msg.payload.length; i++){\n \n let loop_date = msg.payload[i].Zeit\n let date_arr = loop_date.split(\" \") // split the date/time to an array\n let date_year = date_arr[0].split(\".\") // split the date to an array\n let date_time = date_arr[1].split(\":\") // split the time to an array\n // rebuild the date/time into needed format\n loop_date = date_year[2]+\".\"+\n date_year[1]+\".\"+\n date_year[0]+\".\"+\n date_time[0]+\":\"+\n date_time[1]+\":\"+\n date_time[2];\n \n // convert to string and put back in msg\n msg.payload[i].Zeit = new Date(loop_date);\n if(msg.payload[i].Zeit.getTime() < time.getTime()){\n continue; \n }\n tem.payload=msg.payload[i];\n node.send(tem);\n }\n\n return;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":360,"wires":[["1d3eb04061b5dfcf"]]}]

```

---

<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: [14 July 2022 21:17 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/16 "2022-07-14T21:17:27Z")

</div>

@zenofmud Thank you.but it still gives me single output like mine.I wanted it in an array,because so it can not be read by the http respond node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90435f2a2dcc300caeeaaa12146a20f42a3954d4.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: [14 July 2022 21:54 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/17 "2022-07-14T21:54:04Z")

</div>

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

---

<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 06:59 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/18 "2022-07-15T06:59:23Z")

</div>

@zenofmud could you please help me or show me? that is what I was asking from the begining because I didn't succeed doing it. I tried the solution of @HaroldPetersInskipp but I have the same thing

---

<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 07:51 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/19 "2022-07-15T07:51:53Z")

</div>

In my last post I told you what you need to do. If you are having a problem doing it show me what you have tried.

---

<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 08:46 UTC](https://discourse.nodered.org/t/if-statement-in-for-loop/65052/20 "2022-07-15T08:46:43Z")

</div>

@zenofmud this is exactly your code I implemented and I have this as output

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

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