If statement in for loop

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

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

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

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.

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.

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)

1 Like

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:

// 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]

here is it @zenofmud .Tahnks for the advice.

 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;

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) 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

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

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

@zenofmud these are how each object ist

array

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.

{
"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"
}

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

[{"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"]]}]
1 Like

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

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.

@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

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.

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