Filtering arrays with jQuery & linqJS

Filtering arrays in JavaScript can be a pain.  In the past I’ve used loops over the array to pull out elements.  It worked but was error prone and a mess usually.After some research I found two different ways that look promising for future use. Below I’m simply filtering an array of customers by active only.

One is a very simple method in the jQuery library, grep:

var activeCustomers = $.grep(customers, function(cust){
     return cust.Active == true;
});

Also I came across a great library for developers familiar with LINQ

http://linqjs.codeplex.com/.  LinqJs allows you to use the a LINQ like syntax to manipulate arrays in a very clean way.  There are two different ways using this library to write the filtering:

var activeLinq1 = Enumerable.From(customers)
    .Where("$.Active==true").ToArray();

var activeLinq2 = Enumerable.From(customers)
     .Where(function (cust) { return cust.Active == true; }).ToArray();

One thought on “Filtering arrays with jQuery & linqJS

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>