Jump to content

looping...an object


jimfog

Recommended Posts

I am using a for..in loop for going over the properties of an object.It is a data object sent with ajax by the server. It contains responses regarding the server-side validation of a form. The object will contain message(if the validation failed) or empty strings(otherwise).

 

The logic is that messages are found, print them else do something else...say an OK to the user for example:

var printErrors = '';  for (var key in data) {                  if (data[key]!=='') {                printErrors += '<li>' + data[key] + '</li><br>';                  $('#passerror').html(printErrors);                   }                  else{ alert('OK mighty user');}                } 

But there is a problem with the above code. Even if one empty string is found in the data object the else statement runs. The purpose is that the else statement runs only all the responses of the data object are empty strings(that translates to "no errors").

 

I cannot achieve what I want with the code as it is now. And to get a peek to server-side code to understand what I am talking about here is an example:

 trim($oldpasswd)==''? $passwcheck['oldpass']='You have to enter your old password':$passwcheck['oldpass']='';

The above code checks if the user has filled in the "old password" field when he wants to update his password.

 

 

Link to comment
Share on other sites

You already asked a question like this. Use a variable to keep track of whether everything is empty. If you find something that is not empty, set the variable to false. After the loop is finished then decide what you want to do based on whether or not everything was empty. I've already given you one example where you put something inside a loop and expected it to only run once if some condition was true for every item in the loop, that is not how loops work. Run the loop first, then do the check. Anything that you put inside a loop body runs for every item in the loop, that is how loops work. That's the purpose of them.

Link to comment
Share on other sites

Ι used the code found in the other topic and it does not work for my case.

see the code I have made...it is contained in the success function of an ajax call where the results of the data object get processed:

  success:function(data){             var printErrors = '';             var countErrors = true;              for (var key in data) {                  if (data[key]!=='') {//this means error messages must be printed to the browser                                   printErrors += '<li>' + data[key] + '</li><br>';                    countErrors=false;                   }                  if (countErrors ==false) {                     $('#passerror').html(printErrors);                     }                  else{                   alert('hi');//this is for testing only                  }                }                   },
Link to comment
Share on other sites

You still have all of your code inside the loop. The only thing the loop does is loop over whatever you're looping over. You don't want to display error messages every time you loop over something, right? You don't want to display the message that there were no errors every time you loop over something, right? You want the messages to be displayed once, after the loop finishes, so don't put that code inside the loop.

Link to comment
Share on other sites

SORRY...i DID NOT OBSERVED IN YOUR WORDS THIS CRITICAL DETAIL...MY FAULT.

 

You were absolutely right after all.

Edited by jimfog
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...