Nested jQuery.each() - continue/break
There are a lot of answers here. And it's old, but this is for anyone coming here via google. In jQuery each function
return false;
is like break
.
just
return;
is like continue
These will emulate the behavior of break and continue.
As is stated in the jQuery documentation http://api.jquery.com/jQuery.each/
return true
in jQuery.each
is the same as a continue
return false
is the same as a break
You should do this without jQuery, it may not be as "pretty" but there's less going on and it's easier to do exactly what you want, like this:
var sentences = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'Vivamus aliquet nisl quis velit ornare tempor.',
'Cras sit amet neque ante, eu ultrices est.',
'Integer id lectus id nunc venenatis gravida nec eget dolor.',
'Suspendisse imperdiet turpis ut justo ultricies a aliquet tortor ultrices.'
];
var words = ['ipsum', 'amet', 'elit'];
for(var s=0; s<sentences.length; s++) {
alert(sentences[s]);
for(var w=0; w<words.length; w++) {
if(sentences[s].indexOf(words[w]) > -1) {
alert('found ' + words[w]);
return;
}
}
}
You can try it out here. I'm not sure if this is the exact behavior you're after, but now you're not in a closure inside a closure created by the double .each()
and you can return
or break
whenever you want in this case.