首页 > 代码库 > Jquery.linq 使用示例

Jquery.linq 使用示例

 /*Linq JS*/
    //range
    var aa = Enumerable.range(1, 10).toArray();
    var kk = Enumerable.range(1, 10, 3).toArray();
    //select
    kk = Enumerable.range(1, 10).select("value,index=>index + ‘:‘ + value").toArray();
    //where
    kk = Enumerable.range(1, 10).where("y=>y>3").toArray();
    kk = Enumerable.range(1, 10).where("x,y=>y==3").toArray();
    kk = Enumerable.range(1, 10).select("y=>y>3").toArray();

    //from
    var arry = [1, 2, 3, 3, 5, 6, 7, 8, 9, 10];
    kk = Enumerable.from(arry).where("x=>x>3").toArray();
    kk = Enumerable.from(arry).where("$>3").toArray();

    var arryObject = [{ Dept: "MFG", EMP: "425305" },
                      { Dept: "MFG", EMP: "425306" },
                      { Dept: "MFG", EMP: "425307" },
                      { Dept: "FINC", EMP: "425308" },
                      { Dept: "INFO", EMP: "425309" },
                      { Dept: "INFO", EMP: "425311" },
                      { Dept: "INFO", EMP: "425312" },
                      { Dept: "FINC", EMP: "425313" },
                      { Dept: "FINC", EMP: "425314" },
                      { Dept: "FINC", EMP: "425315"}];

    kk = Enumerable.from(arryObject).where("$.Dept==‘MFG‘").select("$.Dept").toArray();
    kk = Enumerable.from(arryObject).where("val,index=>val.Dept==‘MFG‘").select("val=>val.Dept").toArray();
    kk = Enumerable.from(arryObject)
                   .where(function (val, index) { return val.Dept == ‘MFG‘ && val.EMP == ‘425305‘ })
                   .toArray();
    //forEach
    var ss = [];
    Enumerable.from(arryObject).forEach(function (val, index) {
        ss.push(val);
    });

    //distinct
    var qq = Enumerable.from(arryObject)
                       .distinct("x=>x.Dept")
                       .select("x=>x.Dept")
                       .toArray();
    //groupBy
    var newArray = [];
    var oGrop = Enumerable.from(arryObject).groupBy("$.Dept").forEach(function (val) {
        var ee = Enumerable.from(val).toArray();
        var strDept = ee[0].Dept;
        var iLength = ee.length;
        var obj = { Dept: strDept, Length: iLength };
        newArray.push(obj);
    });

    var oGrop = Enumerable.from(arryObject).groupBy("$.Dept").select(function (val) {
        var ee = Enumerable.from(val).toArray();
        var strDept = ee[0].Dept;
        var iLength = ee.length;
        return { Dept: strDept, Length: iLength };
    }).toArray();