Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript/jQuery equivalent of LINQ Any()

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.

like image 555
dilbert Avatar asked May 10 '11 13:05

dilbert


People also ask

Can we use LINQ in JQuery?

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.

Does Javascript have LINQ?

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.


1 Answers

These days you could actually use Array.prototype.some (specced in ES5) to get the same effect:

array.some(function(item) {     return notValid(item); }); 
like image 191
Sean Vieira Avatar answered Sep 17 '22 17:09

Sean Vieira