Is there an equivalent of IEnumerable.Any(Predicate<T>)
in JavaScript or jQuery?
I am validating a list of items, and want to break early if error is detected. I could do it using $.each
, but I need to use an external flag to see if the item was actually found:
var found = false;
$.each(array, function(i) {
if (notValid(array[i])) {
found = true;
}
return !found;
});
What would be a better way? I don't like using plain for
with JavaScript arrays because it iterates over all of its members, not just values.
some()
executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found,some()
immediately returns true. Otherwise,some()
returns false. – Bunkum