用途:方便js操作查詢json資料。
下載網址:http://jslinq.codeplex.com/
使用方法:只需要引用linq.js即可。
查詢方法:
一、where查詢
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 21 }, { Name: "Lilei", Age: 18 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; var arrRes = Enumerable.From(myList).Where("x=>x.Name=='Jim'").ToArray();
二、排序:OrderBy
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 21 }, { Name: "Lilei", Age: 18 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; var arrRes = Enumerable.From(myList).OrderBy("x=>x.Age").ToArray();//降序OrderByDescending()
三、去重:Distinct
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 20 }, { Name: "Lilei", Age: 20 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; var arrRes = Enumerable.From(myList).Distinct("x=>x.Age").ToArray();
四、遍歷:ForEach
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 20 }, { Name: "Lilei", Age: 20 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; Enumerable.From(myList).ForEach(function(value, index){ document.write("值="+value+",索引="+index); });
五、取唯一物件:First、FirstOrDefault、Last、LastOrDefault、Single、SingleOrDefault
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 20 }, { Name: "Lilei", Age: 20 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; var arrRes = Enumerable.From(myList).FirstOrDefault("x=>x.Age>18");
六、Skip、Take
Enumerable.Range(1,10).Skip(5)//結果[6,7,8,9,10] Enumerable.Range(1,10).Take(5)//結果[1,2,3,4,5]