Just a small tip for some people who might be modifying arrays during a loop, where the array is used in the condition of the loop. Just be a little bit careful on what you do with the array when looping through.
var testArray = Array('1','2','3','4','5');
for(var j=0; j<testArray.length; j++) {
alert(testArray[j]);
if(testArray[j] == '3') {
testArray.splice(j,1);
}
}
The above array loop is written in JavaScript, whether you're using JavaScript or not this is still something small to keep in mind. It will produce an alert of '1', then '2', then '3', and then '5'.
The length of the above array is 5, you start at position 0 and testArray[0] doesn't equal '3' so move on, same thing with position 1 (testArray[1] doesn't equal '3'). You then move onto position 2 where testArray[2] does equal '3', so we splice the array at position 2 which removes testArray[2] from the array, making the rest of the array drop down a position.
j still equals 2, so when it loops through again, j will equal 3. When it looks at what position 3 equals (testArray[3]) it will now be '5' as the array's position dropped down by one from the splice.
So we missed out on alerting '4' after going through that loop and you might be doing something important with the loop rather then simply alerting the values, there is 2 ways to get around this. One, put the 'something important' function after the loop in its own separate array. Or two, even easier, if the 'if condition' is true and you modified the array, simply affect the value of j so that the next loop through will be correct (like below).
var testArray = Array('1','2','3','4','5');
for(var j=0; j<testArray.length; j++) {
alert(testArray[j]);
if(testArray[j] == '3') {
testArray.splice(j,1);
j--;
}
}