Use filter(), but work with both results
- by Tomalak
In jQuery, filter() reduces your result to those elements that fulfill a certain condition.
This splits the list in two parts. Working with the "good half" of the elements is easy:
$("some selector").filter(function() {
// determine result...
return result;
}).each( /* do something */ );
But how can I work with the "other half" of my elements, too - but without doing the equivalent of this:
$("some selector").filter(function() {
// determine result...
return !result;
}).each( /* do something else */ );
Basically, I'd like to feed two separate /* do something */ parts to a single filter. One for those that match, and one for the others - without having to filter twice. Am I missing a jQuery function that does this?
P.S.: I guess I could do:
$("some selector").each(function() {
// determine result...
if (result)
/* do something */
else
/* do something else */
});
But I was hoping for something nicer.