65.9K
CodeProject is changing. Read more.
Home

LINQ to JQuery

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (2 votes)

Oct 9, 2013

CPOL
viewsIcon

36223

downloadIcon

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:

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

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

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

//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)));
    }));
};