If statement in for loop

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)

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.

 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;

As I've said before

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

yes.just before the return msg.

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?

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


 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;

Show me what comes out.

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

Try this

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

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

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