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.
By using the JQuery function grep and prototype, it's possible to extend Array class and simulate basic LINQ functions. As shown above in the sample, all functions have the same format.
LINQ in Javascript is a great simple tool for manipulating data collections in javascript. You can easily transform, sort, select and combine data collections using javascript commands in react, angular, etc.
These days you could actually use Array.prototype.some
(specced in ES5) to get the same effect:
array.some(function(item) { return notValid(item); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With