使用多个分组标准 JavaScript (linq.js) 对数据进行分组?
Grouping data with JavaScript (linq.js) with multiple grouping criteria?
我已经阅读 stack overflow 有一段时间了,发现这次我有一个自己的问题 post,因为我找不到答案。我是 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();
感谢您提供的任何帮助,感谢您提供重要的信息!
您有一个复合键,因此您需要使用为键提供比较功能的重载。
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();
我已经阅读 stack overflow 有一段时间了,发现这次我有一个自己的问题 post,因为我找不到答案。我是 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();
感谢您提供的任何帮助,感谢您提供重要的信息!
您有一个复合键,因此您需要使用为键提供比较功能的重载。
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();