我已经读了一段时间的堆栈溢出,发现这次我有自己的问题要发帖,因为我无法找到答案。我对Javascript很陌生,而且我在SQL,Qlikview和VBA的大量工作中遇到了一些困难。
我的问题是如何按照我在SQL中的方式对一堆数据进行分组;从我做过的研究来看,似乎 linq.js 是要走的路,还是有其他好的选择?
我的数据如下:
var data = [
{ ID: I1, Group: G1, Name: A, ArrivalDate: 42097, ReturnDate: 42098, Travellers: 22, City: Boston },
{ ID: I1, Group: G1, Name: A, ArrivalDate: 42098, ReturnDate: 42099, Travellers: 22, City: New York },
{ ID: I1, Group: G1, Name: A, ArrivalDate: 42099, ReturnDate: 42100, Travellers: 22, City: Chicage },
{ ID: I2, Group: G1, Name: A, ArrivalDate: 42097, ReturnDate: 42098, Travellers: 14, City: Boston },
{ ID: I2, Group: G1, Name: A, ArrivalDate: 42098, ReturnDate: 42099, Travellers: 14, City: New York },
{ ID: I2, Group: G1, Name: A, ArrivalDate: 42099, ReturnDate: 42100, Travellers: 14, City: Chicage },
{ ID: I3, Group: G2, Name: B, ArrivalDate: 42091, ReturnDate: 42092, Travellers: 21, City: Boston },
{ ID: I3, Group: G2, Name: B, ArrivalDate: 42092, ReturnDate: 42093, Travellers: 21, City: New York },
{ ID: I3, Group: G2, Name: B, ArrivalDate: 42093, ReturnDate: 42094, Travellers: 21, City: Chicage },
{ ID: I4, Group: G2, Name: B, ArrivalDate: 42091, ReturnDate: 42092, Travellers: 22, City: Boston },
{ ID: I4, Group: G2, Name: B, ArrivalDate: 42092, ReturnDate: 42093, Travellers: 22, City: New York },
{ ID: I4, Group: G2, Name: B, ArrivalDate: 42093, ReturnDate: 42094, Travellers: 22, City: Chicage },
{ ID: I5, Group: G3, Name: C, ArrivalDate: 42082, ReturnDate: 42083, Travellers: 30, City: Boston },
{ ID: I5, Group: G3, Name: C, ArrivalDate: 42083, ReturnDate: 42084, Travellers: 30, City: New York },
{ ID: I5, Group: G3, Name: C, ArrivalDate: 42084, ReturnDate: 42085, Travellers: 30, City: Chicage },
{ ID: I6, Group: G3, Name: C, ArrivalDate: 42082, ReturnDate: 42083, Travellers: 10, City: Boston },
{ ID: I6, Group: G3, Name: C, ArrivalDate: 42083, ReturnDate: 42084, Travellers: 10, City: New York },
{ ID: I6, Group: G3, Name: C, ArrivalDate: 42084, ReturnDate: 42085, Travellers: 10, City: Chicage }
]
在SQL中,我会这样做:
Select ID,
Group,
Name,
min(ArrivalDate) as ArrivalDate,
max(ReturnDate) as ReturnDate,
Travellers
from data
group by ID, Group, Name, Travellers
输出结果为:
[
{ ID: I1, Group: G1, Name: A, ArrivalDate: 42097, ReturnDate: 42100, Travellers: 22 }
{ ID: I2, Group: G1, Name: A, ArrivalDate: 42097, ReturnDate: 42100, Travellers: 14 }
{ ID: I3, Group: G2, Name: B, ArrivalDate: 42091, ReturnDate: 42094, Travellers: 21 }
{ ID: I4, Group: G2, Name: B, ArrivalDate: 42091, ReturnDate: 42094, Travellers: 22 }
{ ID: I5, Group: G3, Name: C, ArrivalDate: 42082, ReturnDate: 42085, Travellers: 30 }
{ ID: I6, Group: G3, Name: C, ArrivalDate: 42082, ReturnDate: 42085, Travellers: 10 }
]
直观地说,这是我想用linq.js写的:
var datagroup = Enumerable.From(data)
.GroupBy("$.id", null,
function (key, g) {
return {
id: key,
group: g("$.group"),
name: g("$.name"),
arrivaldate: g.min("$.arrivaldate"),
returndate: g.max("$.returndate"),
travellers: g("$.travellers")
}
})
.ToArray();
感谢您提供的任何帮助都是一个很好的信息来源!
答案 0 :(得分:0)
您有一个复合键,因此您需要使用为键提供比较功能的重载。
var query = Enumerable.From(data)
.GroupBy(
// key composed of ID, Group, Name, and Travellers
"{ ID: $.ID, Group: $.Group, Name: $.Name, Travellers: $.Travellers }",
null,
"{ ID: $.ID, Group: $.Group, Name: $.Name, ArrivalDate: $$.Min('$.ArrivalDate'), ReturnDate: $$.Max('$.ReturnDate'), Travellers: $.Travellers }",
// compare keys using this string representation
"[ $.ID, $.Group, $.Name, $.Travellers ].join('|')")
.ToArray();