Skip to content


Overview: jLinq

jLinq is a Javascript library that lets you write queries against arrays of javascript objects and JSON data.

Syntax and Examples

Syntax for jLinq is much similar to LINQ ,

var results = jLinq.from(data.users)
.startsWith("first", "a")
.orEndsWith("y")
.orderBy("admin", "age")
.select();

Some important commands

  • .startsWith( field?, value ): Check to see if the field starts with the specified value.
    //Some sample queries
    var results = jLinq.from(records)
    .startsWith("firstname", "jo") // first name starting with 'jo'
    .or("ji") // or first name starting with 'ji'
    
    var results = jLinq.from(records)
    //you can use an array of values to check
    .startsWith("firstname", ["a","e","i","o","u"])
    
  • .endsWith( field?, value ) : Check to see if the field starts with the specified value.
  • .contains( field?, value ) : Check to see if the field contains the specified value.
  • .match( field?, regexp ): Check to see if the
    value matches the regular expression. This command will still take case
    sensitivity into account.
  • .empty( field? ): Check to see if the field contains any value.
  • .greater( field?, value ): Check to see if the field is less than the provided value.

If the field is…

- a string, checks if it ends with the value
- an array, checks if the last element matches the value
- else, convert to a string and check returns jLinq Object

For all commands with examples see this.

Retrieve and Display Results

results[index]['field'] will retrieve values from result object.

Following e.g. will retrieve data and displays in table,

var results = jLinq.from(data.users)
.startsWith("first", "a")
.toTable();
document.write(results);

Creating A Query Command

We can also create our own commands,

jLinq.extend({
    name:"evenValues", //the name of this command i.e. method
    type:"query", //the kind of command this is
    count:0, //how many parameters we expect
    method:function(query) {
        //determine how to evaluate the value being compared
        var compare = query.when({
            "string":function() {
                return query.value.length;
            },
            "array":function() {
                return query.value.length;
            },
            "other":function() {
                return query.helper.getNumericValue(query.value);
            }
        });
        //the actual comparison
        return compare % 2 == 0;
    }
});

E.g using custom command,

//this method is one way to do it
jLinq.from(data.users)
    .less("age", 30)
    .or()
    .evenValues()
    .select();

When you extend a query command , several additional operator prefixed name command are added to jLinq as well. Each of these perform the correct switches when they are used.

//this orEvenValues command is generated automatically

jLinq.from(data.users)
    .less("age", 30)
    .orEvenValues()
    .select();
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Twitter
  • Yahoo! Buzz

Posted in Javascripts, Linq.

Tagged with , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Hugo says

    Glad to see someone is using jLinq!

    jLinq has a selection method named “toTable()” that will return a string of HTML of the results in TABLE format so it might save you some time.

  2. Amol says

    @Hugo, Thanks for the information , I have updated the above example.



Some HTML is OK

or, reply to this post via trackback.