I want to iterate over two arrays at the same time, as the values for any given index i in array A corresponds to the value in array B.
I am currently using this code, and getting undefined when I call alert(queryPredicates[i]) or alert(queryObjects[i]).
I know my array is populated as I print out the array prior to calling this.
//queryPredicates[] and queryObjects[] are defined above as global vars - not in a particular function, and I have checked that they contain the correct information.
function getObjectCount(){
var variables = queryPredicates.length; //the number of variables is found by the length of the arrays - they should both be of the same length
var queryString="count="+variables;
for(var i=1; i<=variables;i++){
alert(queryPredicates[i]);
alert(queryObjects[i]);
}
The value of the
lengthproperty of any array, is the actual number of elements (more exactly, the greatest existing index plus one).If you try to access this index, it will be always
undefinedbecause it is outside of the bounds of the array (this happens in the last iteration of your loop, because thei<=variablescondition).In JavaScript the indexes are handled from
0tolength - 1.Aside of that make sure that your two arrays have the same number of elements.