2014-09-29 34 views
0

我有以下的数组: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 } 
       ] 
      } 
     ]; 

回答

2

组的过载你想要看起来像这样:

// Overload:function(keySelector,elementSelector,resultSelector,compareSelector) 

首先,你需要密钥是分配器ID和名称的组合。 然后收集具有相同经销商编号和名称的所有年份和月份。 当然的结果和比较关键对象的方法(作为字符串的属性是实现这一点的简单方法)。

var query = Enumerable.from(data) 
    .groupBy(
     "{ Id: $.DistributorId, Name: $.DistributorName }", 
     "{ Year: $.Year, Month: $.Month }", 
     "{ DistributorId: $.Id, DistributorName: $.Name, PriceLists: $$.toArray() }", 
     "String($.Id) + $.Name" 
    ) 
    .toArray(); 

只是注意,我使用的linq.js 3.x的名字:方法使用lowerCamelCase。更旧版本的UpperCamelCase。

+0

它的工作原理,谢谢 - 在使用linq.js 3.x时,您是否面临任何问题,因为它仍然是测试版? – Taraman 2014-09-30 07:50:58

+0

我不能说我有。大部分情况完全相同。我没有真的使用新的方法,但我没有理由认为他们不会工作。 – 2014-09-30 13:53:35

+0

如果我想返回计数,使我的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

0

这应该做的伎俩:

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 } 
]; 

var dest = []; 
source.map(function(current){ 
    var id = current.DistributorId - 1; 
    dest[id] = dest[id] || { 
     "DistributorId": current.DistributorId, 
     "DistributorName": current.DistributorName, 
     "PriceLists": [] 
    }; 
    dest[id].PriceLists.push({ "Year": current.Year, "Month": current.Month }); 
})