我有以下的数组:Linq.js:分组两个属性(字段)
var source = [
{ "DistributorId": 1, "DistributorName": "Distributor 01", "PriceListId": 1, "Year": 2014, "Month": 9 },
{ "DistributorId": 1, "DistributorName": "Distributor 01", "PriceListId": 2, "Year": 2014, "Month": 10 },
{ "DistributorId": 2, "DistributorName": "Distributor 02", "PriceListId": 3, "Year": 2014, "Month": 10 },
{ "DistributorId": 3, "DistributorName": "Distributor 03", "PriceListId": 4, "Year": 2014, "Month": 9 },
{ "DistributorId": 3, "DistributorName": "Distributor 03", "PriceListId": 5, "Year": 2014, "Month": 10 }
];
欲由两个场使用linq.js到组这些阵列“DistributorId”和“DistributorName” 得到以下结果:
var des =
[
{
"DistributorId": 1,
"DistributorName": "Distributor 01",
"PriceLists":
[
{ "Year": 2014, "Month": 9 },
{ "Year": 2014, "Month": 10 }
]
},
{
"DistributorId": 2,
"DistributorName": "Distributor 02",
"PriceLists":
[
{ "Year": 2014, "Month": 10 }
]
},
{
"DistributorId": 3,
"DistributorName": "Distributor 03",
"PriceLists":
[
{ "Year": 2014, "Month": 9 },
{ "Year": 2014, "Month": 10 }
]
}
];
它的工作原理,谢谢 - 在使用linq.js 3.x时,您是否面临任何问题,因为它仍然是测试版? – Taraman 2014-09-30 07:50:58
我不能说我有。大部分情况完全相同。我没有真的使用新的方法,但我没有理由认为他们不会工作。 – 2014-09-30 13:53:35
如果我想返回计数,使我的ToArray返回以下数组对象 [{“DistributorId”:1,“DistributorName”:“Distributor 01”,Total:2} {“DistributorId”:1, DistributorName“:”Distributor 02“,Total:1} {”DistributorId“:1,”DistributorName“:”Distributor 03“,Total:2} ] – nav 2015-01-28 16:10:52