Introduction
A few days ago on the intersection of a JS array to ask the next colleague, his first reaction is the cycle, which is also the conventional thinking, because I personally more inclined to JS, I joked, I do not know JS can be like LINQ to achieve these operations? Decisive Baidu has a, sure enough there is a frame ah, is really a master in the folk ah.
The official introduction says that support for more than 90 methods, I feel at least C # LINQ has a way it certainly has.
Advantages
1, support jquery plug-in way. Jquery.linq.min.js.
2, can also be used like ordinary JS method. Linq.min.js.
3, of course, with customary vs children's shoes certainly want to have a good sense of intelligence, yes, it supports.
4, and C # LINQ has a very similar place, this will let you get started faster, but also I am happy to be in some of the methods of Linq.js, you can write as a lambda expression to write the filter, and in the filter string to support the $ symbol "under".
Linq.js structure diagram
Experience One: Querying JSON objects
function testqueryobjects () {var jsonarray = [{"Stuid": 1, "Stuname": "James", "age": +, "Co Untry ":" USA "}, {" Stuid ": 2," Stuname ":" Byant "," Age ": $," Country ":" USA "}, {" Stuid ": 3, "Stuname": "Lin", "age": +, "country": "Taiwan"}, {"Stuid": 4, "Stuname": "Yao", "age": +, "country ":" Shanghai "}]; var Querresult = $. Enumerable.from (Jsonarray). Where (function (x) {return x.age <= 30;}) . (function (x) {return x.stuid;}) . ToArray (). ForEach (function (i) {document.write (I.stuid + ";" + I.stuname + ";" + i.age + ";" + I.country + "<br/>"); }); document.write ("====================================" + "<br/>"); var queryResult2 = $. Enumerable.from (Jsonarray). Where ("$". Age<=30 ") //This operation is very bullish. ("$. Stuid "). ToArray (). ForEach (function (i) {document.write (I.stuid + ";" + I.stuname + ";" + i.age + ";" + I.country + "<br/>"); }); }
Two queryresult will produce the same result.
Experience Two: How does Lambda come, linq.js?
function Testwithlinq () { //C # Linq //enumerable.range (1, ten)// . Where (delegate (int i) {return I% 3 = = 0;}) //. Select (delegate (int i) {return i *;}); Enumerable.range (1, ten) . Where (function (i) {return I% 3 = = 0;}) . Select (function (i) {return i *;}) . ToArray () . ForEach (function (i) { document.write (i + ";");//30;60;90; });}
Experience Three: Go heavy
function testdistinct () { var arr = [N, N, A, a, 500,200, +]; Enumerable.from (arr) . Distinct () . ToArray () . ForEach (function (i) { document.write (i + ";");//100;200;30;40;500; }); }
Experience Four: intersection, difference set, and set
function testintersectexcept () { var arr1 = [1, 412, +--]; var = [arr2, 25,22,20]; Enumerable.from (ARR1). Intersect (ARR2). ForEach (function (i) { document.write (i + ";");//53,25 }); Enumerable.from (ARR1). Except (ARR2). ForEach (function (i) { document.write (i + ";");//1;412 }); Enumerable.from (ARR1). Union (ARR2). ForEach (function (i) { document.write (i + ";");//1;412;53;25;22;20; }); }
Experience Five: Alternate, Contains
function Testalternate () { enumerable.range (1, 5). Alternate (' * '). ForEach (function (i) { document.write (i + ";");//;*;2;*;3;*;4;*; }); var r = Enumerable.range (1, 5). Contains (3); document.write (R); Ture }
Experience Six: Min, Max
function Testmaxmin () { var max = Enumerable.range (1, 5). Max (); 5 var min = enumerable.range (1, 5). Min (); 1 document.write (max+ ";") +min); }
Summarize
Can use these JS places some functions can also be implemented in the background, but I personally prefer JS way, so use the above several methods, in addition to see Linq.js source code for some algorithms can also deepen the rationale. Usage and hobbies are based on personal interests, but it's a bit shocking when I find this framework.
GitHub Address: http://linqjs.codeplex.com/
The official API and demo can be very simple: http://neue.cc/reference.htm
If you are also a JS fan or feel the help you have, please feel good, haha.
"JavaScript" another artifact frame: linq.js