用途:方便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]
如果本文对你有所帮助,请打赏——1元就足够感动我:)
联系邮箱:intdb@qq.com
我的GitHub: https://github.com/vipstone
联系邮箱:intdb@qq.com
我的GitHub: https://github.com/vipstone
关注公众号:
作者: 王磊
出处: http://vipstone.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,请标明出处。