linqjs试用

 1 var data = {
 2     customers: [{
 3         id: 1,
 4         firstname: 'John',
 5         lastname: 'Smith',
 6         sex: 'male'
 7     }, {
 8         id: 2,
 9         firstname: 'Jessica',
10         lastname: 'Johnson',
11         sex: 'female'
12     }, {
13         id: 3,
14         firstname: 'Michael',
15         lastname: 'Williams',
16         sex: 'male'
17     }, {
18         id: 4,
19         firstname: 'Amber',
20         lastname: 'Davis',
21         sex: 'female'
22     }],
23     address: [{
24         id: 1,
25         address: 'ynkm'
26     }, {
27         id: 2,
28         address: 'ynyx'
29     }]
30 };
31 
32 var enumerable=Enumerable.from(data.customers);
33 
34 enumerable.groupBy("$.firstname.charAt(0)", "x=>x.firstname").forEach(function(group) {
35     console.log(group.key());
36     group.forEach(function(aaa) {
37         console.log(aaa);
38     });
39 });
40 
41 
42 enumerable.groupBy("$.firstname.charAt(0)",  "","key,group=>key+'-'+group.select('$.firstname').toJoinedString(',')").forEach(function(group) {
43     console.log(group);
44 });
45 
46 enumerable.toLookup("$.sex","$.firstname").toEnumerable().forEach(function(group) {
47     console.log(group.key());
48     group.orderBy().forEach(function(aaa) {
49         console.log(aaa);
50     });
51 });
52 
53 console.log(enumerable.toDictionary("$.firstname").get("Richard"));
54 console.log(enumerable.toObject("$.firstname")["Richard"]);
55 
56 var enumerable2=Enumerable.from(data.address);
57 //inner join
58 enumerable.join(enumerable2, "$.id", "$.id", "outer,inner=>{'id':outer.id,'firstname':outer.firstname,'address':inner.address}").forEach(function(aaa) {
59     console.log(aaa);
60 });
61 
62 enumerable.groupJoin(enumerable2, "$.id", "$.id", "outer,inner=>{'id':outer.id,'firstname':outer.firstname,'address':inner&&inner.address||''}").forEach(function(aaa) {
63     console.log(aaa);
64 });

 

posted @ 2017-05-29 01:33  godghdai  阅读(953)  评论(0编辑  收藏  举报