Click here to Skip to main content
15,906,288 members
Articles / Programming Languages / Javascript
Tip/Trick

LINQ to JQuery

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
19 Nov 2013CPOL 35.7K   7   1
Extends Arrays in JavaScript, adding the main domain and filtering functions of LINQ

Introduction

This code extends Arrays in JavaScript, adding the main domain and filtering functions of LINQ, Count(), First(), Exists() and Where().

Background

By using the JQuery function grep and prototype, it's possible to extend Array class and simulate basic LINQ functions.

Sample:

Java
myArray.Count("['name']=='luis'") 

As shown above in the sample, all functions have the same format.

JavaScript
stringObject.FunctionName(stringFilter) 

stringFilter may be a complex logic sentence, functions will execute by using eval functions.

The code is very easy to understand, as shown below.

Using the Code

JavaScript Code

JavaScript
//LinQ to javascript extensions methods for class Array
//Need JQuery


// *** Samples of use ****

    var myArray = [
                     { id: 1, name: "luis", checked: false }
                    , { id: 2, name: "juan", checked: true }
                    , { id: 3, name: "ana", checked: false }
    ];


    //console.log("Count sample: " + myArray.Count("item['checked']==true || item['id']==1"));
    ////word item it's not necessary
    //console.log("Count sample: " + myArray.Count("['checked']==true || ['id']==1"));
    //console.log("Count sample: " + myArray.Count("['name']=='luis'"));
    ////If not params, return array length
    //console.log("Count sample: " + myArray.Count());

    //console.log("First sample: " + myArray.First("['id']==1").id);
    //console.log("First sample: " + myArray.First().id);

    //console.log("Exists and First sample: " + (myArray.Exists("['name']=='anaXX'")
    //                                            ? myArray.First("['name']=='anaXX'").id
    //                                            : "not exits"));

    //var vectResult = myArray.Where("item['id']==2");

//*** Samples of use ****



function itemParser(str) {
    var result;

    result = str.replace(/item/gi, "");
    result = result.replace(/\[/g, "item[");

    return result;
}


//Extend Array with prototype

Array.prototype.Count = function (expr) {
    if (expr == null || expr == "") return this.length;

    return ($.grep(this, function (item, index) {
        return (eval(itemParser(expr)));
    }).length);
};


Array.prototype.First = function (expr) {
    if (expr == null || expr == "") expr = "true";

    var result = ($.grep(this, function (item, index) {
        return (eval(itemParser(expr)));
    }));

    return (result.length > 0 ? result[0] : null);
};


Array.prototype.Exists = function (expr) {
    if (expr == null || expr == "") expr = true;

    var result = ($.grep(this, function (item, index) {
        return (eval(itemParser(expr)));
    }));

    return (result.length > 0);
};


Array.prototype.Where = function (expr) {
    return ($.grep(this, function (item, index) {
        return (eval(itemParser(expr)));
    }));
};

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLINQjs is far better Pin
Jaydeep Jadav10-Oct-13 3:49
Jaydeep Jadav10-Oct-13 3:49 
LINQjs is far better and having almost all functions.

http://linqjs.codeplex.com/[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.