用途:方便js操作查询json数据。
下载网址:http://jslinq.codeplex.com/
使用方法:只需要引用linq.js即可。
查询方法:
一、where查询
1
2
3
4
5
6
7
8
|
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
1
2
3
4
5
6
7
8
|
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
1
2
3
4
5
6
7
8
|
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
1
2
3
4
5
6
7
8
9
10
|
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
1
2
3
4
5
6
7
8
|
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
1
2
|
Enumerable.Range(1,10).Skip(5) //结果[6,7,8,9,10]
Enumerable.Range(1,10).Take(5) //结果[1,2,3,4,5]
|
本文转自王磊的博客博客园博客,原文链接:http://www.cnblogs.com/vipstone/p/5588020.html,如需转载请自行联系原作者