By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

How to verify that all objects in an array meet a single requirement?

P: 2
I am very new to javascript (and programming in general, for that matter), and I am having difficulty reworking this bit of code:

Expand|Select|Wrap|Line Numbers
  1. if ((a1 == 'valid') && (a2 == 'valid') && (a3 == 'valid') && (a4 == 'valid')) {
  2.     // do something
  3. }
  4.  
Once I create the array...

Expand|Select|Wrap|Line Numbers
  1. var theArray = [a1, a2, a3, a4]
  2.  
...how do I loop through and check to see if each object in the array (not just one) is valid? For instance:

Expand|Select|Wrap|Line Numbers
  1. if (theArray[all] == 'valid') {
  2.     // do something
  3. }
  4.  
I suspect the answer to this question is somewhat rudimentary, but I was unable to find it anywhere else in the forum. Perhaps I am not using the correct terms in my search? In any case, any guidance would be much appreciated.

Thanks,
Matthew
Apr 14 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 392
...how do I loop through and check to see if each object in the array (not just one) is valid? For instance:
Expand|Select|Wrap|Line Numbers
  1.     var theArray = [a1, a2, a3, a4]
  2.     for( var i = 0; i < theArray.length; i++ ) {
  3.         var someValue = theArray[i];
  4.         if ( someValue == 'valid' ) {
  5.             // do something
  6.         }
  7.     }
  8.  
  9. }
  10.  
Apr 14 '08 #2

P: 2
Thanks for the response. I must be dumber than I realized, because I am still unable to get my code to work. Please have a look at what I am working with:

Expand|Select|Wrap|Line Numbers
  1. function updateMaster() {
  2.     var opt0 = parent.document.getElementById('opt0').value
  3.     var opt1 = parent.document.getElementById('opt1').value
  4.     var opt2 = parent.document.getElementById('opt2').value
  5.     var optArray = new Array(opt0,opt1,opt2)
  6.     for (i=0; i <= 11; i++) {
  7.         var x = optArray[i]
  8.         // if ALL are CHECKED
  9.         if (x == '0') {
  10.             parent.document.getElementById('clickValue').value = 'reset'
  11.             }
  12.         // if ALL are UNCHECKED
  13.         else if (x != '0') {
  14.             parent.document.getElementById('clickValue').value = '9'
  15.         }
  16.         // if SOME are UNCHECKED
  17.         else {
  18.             parent.document.getElementById('clickValue').value = '-9'
  19.         }
  20.     }
  21. }
  22.  
I have tried a million variations of this, but each and every time, no matches are found and '9' is returned. Now, my morale has sunken low enough to warrant another shameless cry for help.

Any suggestions? Thanks again.
Apr 17 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.