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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcg365
    New Member
    • Apr 2008
    • 2

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

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

    Code:
    if ((a1 == 'valid') && (a2 == 'valid') && (a3 == 'valid') && (a4 == 'valid')) {
        // do something
    }
    Once I create the array...

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

    Code:
    if (theArray[all] == 'valid') {
        // do something
    }
    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
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    Originally posted by mcg365
    ...how do I loop through and check to see if each object in the array (not just one) is valid? For instance:
    Code:
        var theArray = [a1, a2, a3, a4]
        for( var i = 0; i < theArray.length; i++ ) {
            var someValue = theArray[i];
            if ( someValue == 'valid' ) {
                // do something
            }
        }
    
    }

    Comment

    • mcg365
      New Member
      • Apr 2008
      • 2

      #3
      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:

      Code:
      function updateMaster() {
      	var opt0 = parent.document.getElementById('opt0').value
      	var opt1 = parent.document.getElementById('opt1').value
      	var opt2 = parent.document.getElementById('opt2').value
      	var optArray = new Array(opt0,opt1,opt2)
      	for (i=0; i <= 11; i++) {
      		var x = optArray[i]
      		// if ALL are CHECKED
      		if (x == '0') {
      			parent.document.getElementById('clickValue').value = 'reset'
      			}
      		// if ALL are UNCHECKED
      		else if (x != '0') {
      			parent.document.getElementById('clickValue').value = '9'
      		}
      		// if SOME are UNCHECKED
      		else {
      			parent.document.getElementById('clickValue').value = '-9'
      		}
      	}
      }
      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.

      Comment

      Working...