linq to js使用汇总
生活随笔
收集整理的這篇文章主要介紹了
linq to js使用汇总
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用途:方便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]
?
總結
以上是生活随笔為你收集整理的linq to js使用汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL 索引的面试题总结
- 下一篇: switch 的性能提升了 3 倍,我只