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.
- <script src="https://code.google.com/p/jlistquery/source/browse/trunk/jListQuery.js" type="text/javascript"></script>
- @section
- Scripts {
- @Scripts.Render("https://code.google.com/p/jlistquery/source/browse/trunk/jListQuery.js")
- }
| 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);
employee = $.firstOrDefault(function (e) { return e.Name == "Shan"; });
alert(employee.Name);
//Loop through all elements of the list



Join the conversation! Your thoughts help the community grow.