I have the following construct:
Note: the arrays and the goal in this example are only symbolic! I know that there would be way better solutions for this example - it's just to demonstrate the construct of the code.
var firstArray = [1,2,3,4,5];
var secondArray = [1,2,4,5];
$.each(firstArray,function(i,firstArrayElement){
$.each(secondArray,function(i,secondArrayElement){
if(firstArrayElement === secondArrayElement) {
// do stuff
// PROBLEM: force the firstArray loop to continue with the next iteration
}
});
console.log("Didn't find: "+firstArrayElement);
});
To clarify my question: Is there a way to force the parent .each to continue (= skip the console.log
) with the next iteration? In PHP this would be continue 2;
. So the goal is, to never reach the console.log()
if a condition is true (in this example an element is found).
There is the flag solution (jsFiddle), but there has to be a nicer way to do this.
There are also solutions with labels but labels won't work with .each().
So what I am looking for is probably a way to use return
in the parent function without flags. For the example above, this would mean that the result logs: Didn't find 3