How to I break out of the following jquery each method when a condition is met;
var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};
$.each(rainbow, function (key, color) {
if (color == 'red') {
//do something and then break out of the each method
alert("i'm read, now break.");
}
});
We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.