Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

LINQ to JQuery

4.50/5 (2 votes)
19 Nov 2013CPOL 36.2K   2  
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)