Purpose

jListQuery is a library that provides a fluent interface to query JSON/JavaScript lists/arrays in memory.

Implementation

jListQuery is implemented as a jQuery plugin. You can download the plugin for your own use.

You can include jListQuery in your html file from googlecode by adding the below script tag to your page:
  1. <script src="https://code.google.com/p/jlistquery/source/browse/trunk/jListQuery.js" type="text/javascript"></script>
In ASP .NET MVC 4 view:
  1. @section
  2. Scripts {
  3. @Scripts.Render("https://code.google.com/p/jlistquery/source/browse/trunk/jListQuery.js")
  4. }
A demo of the plugin is on JSFiddle:
http://jsfiddle.net/7B932/2/
The plugin has been hosted by the jQuery Plugin Registry at:
http://plugins.jquery.com/tag/jlistquery/
API Description Parameters
list Initialize the querying interface with a JSON/JavaScript list. list: the list to initialize with
join Join another list with exisiting list or previous join. list: list to join with
on: function with the condition of the join
forEach Iterates through list and executes an action against each item. action: the action to execute for each element in the list
firstOrDefault Fetches the first item that matches the predicate or returns null. predicate: function with the condition
lastOrDefault Fetches the last item that matches the predicate or returns null. predicate: function with the condition
singleOrDefault Fetches the item if it is the only item in the list that matches the predicate or returns null. predicate: function with the condition
where Fetches all items from the list that match the predicate. predicate: function with the condition
toList Re-initialize the result of a query to a new list or return the result of the previous query. This is to be used with the where and orderBy APIs. reinitialize: boolean, true initializes a new list and false returns the result.
orderBy Orders the list by a predicate. predicate: function with the condition for ordering
select Returns a list of elements that are the result of the query. predicate: function that returns a new element
removeAll Removes all elements that match the predicate. predicate: function with the condition

Samples

//Json Data

var listEmployees = { "employees": [{ "Id": 3, "Name": "David" }, { "Id": 2, "Name": "Adam" }, { "Id": 1, "Name": "Shan"}] };

var listSuper = { "funds": [{ "EmpId": 3, "FundName": "123 Super" }, { "EmpId": 2, "FundName": "XYZ Super" }, { "EmpId": 1, "FundName": "ABC Super"}] };

var listDetails = { "details": [{ "EmpId": 3, "Address": "123 Street", "Dob": "1995/07/21" }, { "EmpId": 1, "Address": "ABC Street", "Dob": "1983/06/21" }, { "EmpId": 2, "Address": "XYZ Street", "Dob": "1995/06/21"}] };

//Initialize list

$.list(listEmployees.employees);

//Query list

var employee = $.singleOrDefault(function (e) { return e.Name == "Adam"; });

alert(employee.Name);

Json List

employee = $.firstOrDefault(function (e) { return e.Name == "Shan"; });

alert(employee.Name);

Query list

//Loop through all elements of the list

$.forEach(function (e, index) { alert(e.Name); });

Loop

//Query to get all employees where the Id is greater than or equal to 2

var employees = $.list(listEmployees.employees)

.where(function (e) { return e.Id >= 2 }).toList(false);

for (var i = 0; i < employees.length; i++) {

alert(employees[i].Id + "," + employees[i].Name);

}

For loop in list

//Query to get all employees where the Id is greater than or equal to 2, ordered by Name
var employees = $.list(listEmployees.employees)
.where(function (e) { return e.Id >= 2 }).toList(true)
.orderBy(function (a, b) { return a.Name < b.Name ? -1 : a.Name > b.Name ? 1 : 0; })
.toList(false);
for (var i = 0; i < employees.length; i++) {
alert(employees[i].Id + "," + employees[i].Name);
}

//Query to get all employees (and their details) whose Date of Birth is greater than 1990/01/01, ordered by Name

var employees =

$.list(listEmployees.employees)

.join(listSuper.funds, function (emp, superAnn) { return emp.Id == superAnn.EmpId; })

.join(listDetails.details, function (lastJoin, detail) { return lastJoin[0].Id == detail.EmpId; })

.where(function (joinedItem) { return new Date(joinedItem[2].Dob) > new Date(1990, 1, 1) })

.toList(true)

.orderBy(function (a, b) { return a[0].Name < b[0].Name ? -1 : a[0].Name > b[0].Name ? 1 : 0; })

.select(function (joinedItem) {

var item = {};

item.Id = joinedItem[0].Id;

item.Name = joinedItem[0].Name;

item.Super = joinedItem[1].FundName;

item.Address = joinedItem[2].Address;

item.Dob = joinedItem[2].Dob;

return item;

});

for (var i = 0; i < employees.length; i++) {

alert(employees[i].Id + "," + employees[i].Name + "," + employees[i].Super + "," + employees[i].Address + "," + employees[i].Dob);

}

Query to get all employees
//Remove all employees whose name matches Adam

$.list(employees)

.removeAll(function (emp) { return emp.Name == "Adam"; });

for (var i = 0; i < employees.length; i++) {

alert(employees[i].Id + "," + employees[i].Name + "," + employees[i].Super + "," + employees[i].Address + "," + employees[i].Dob);

}

Remove all employees