2009-06-26 49 views
4
IList<Customer> Customers = 
      flat.GroupBy(cust => new { cust.ReferenceNumber, cust.Name, cust.Address }) 
       .Select(c => new Customer() 
       { 
        ReferenceNumber = c.Key.ReferenceNumber, 
        Name = c.Key.Name, 
        Address = c.Key.Address, 
        Orders = c.Select(o => new Order() 
        { 
         OrderId = o.OrderId, 
         ProductName = o.ProductName, 
         Description = o.Description, 
         Amount = o.Amount 
        }).ToList() 
       }).ToList() 

正在採取一個扁平列表並將其轉換爲一個嵌套的對象可能在Javascript中? 一個通用的解決方案是??在C#中創建嵌套的對象在javascript像groupby#

+0

傳說!只是解決了我的問題關於使用underscore.js分組[10022156] [1] [1]:http://stackoverflow.com/questions/10022156/underscore-js-groupby-multiple-values – rickysullivan 2012-04-16 04:22:54

回答

3

是的。

您可以在JavaScript中傳遞函數,這與C#代碼中的lambda表達式具有相同的用途。就拿使用jQuery的「每個」作爲一個例子:

$('div.several').each(function() { 
    // Do something with the current div. 
}); 

您還可以創建嵌套的對象很容易:

var outer = { 
    inner1: { 
     val1: 'a', 
     val2: 'b' 
    }, 
    inner2: { 
     val1: 'c', 
     val2: 'd' 
    } 
}; 

當然,你也可以做到這一點動態的,而不是一下子:

var outer = {}; 
outer.inner1 = {}; 
outer.inner1.val1 = 'a'; 
... 

然後,你要查找的內容,你需要使用數組:

var result = []; 
for (var i=0; i<x; ++i) { 
    result[result.length] = GetIndividualResult(i); 
} 
+0

不完全我需要什麼,但有一些解決方案的基石。乾杯。 – Schotime 2009-06-29 23:27:12

16

儘管這是一個老問題,我想我會提供了一個更優雅的解決方案:

/** 
* Groups an array of objects by one or more keys 
* 
* @param array arr  The array of objects to group 
* 
* @param string|function A string representing the child property to group by 
*      or a function that returns an array of one or more properties. 
* 
* @returns    An object with keys representing the grouping properties, 
*      finally holding an array of records that fell into 
*      those groups. 
*/ 
var group = function(items, by) { 
    var groups = {}, 
     group, 
     values, 
     i = items.length, 
     j, 
     key, 
     group_keys; 

    // make sure we specified how we want it grouped 
    if(!by) { return items; } 
    while(i--) { 

     // find out group values for this item 
     values = (typeof(by) === "function" && by(items[i]) || 
        typeof items[i] === "object" && items[i][by] || 
        items[i]); 

     // make sure our group values are an array 
     values = values instanceof Array && values || [ values ]; 

     // recursively group 
     group = groups; 
     for(j = 0; j < values.length; j++) { 
      key = values[j]; 
      group = (group [key] || (group [key] = j === values.length - 1 && [] || {})); 
     } 

     // for the last group, push the actual item onto the array 
     group = (group instanceof Array && group || []).push(items[i]); 
    } 

    return groups; 
}; 

與此調用它:

var items = [ 
    { "id" : 1, "name" : "foo", "category" : "a" }, 
    { "id" : 2, "name" : "foo", "category" : "a" }, 
    { "id" : 3, "name" : "bar", "category" : "b" }, 
    { "id" : 4, "name" : "free", "category" : "a" }, 
    { "id" : 5, "name" : "beer", "category" : "b" }, 
    { "id" : 6, "name" : "foo", "category" : "b" } 
]; 

var groups = group(items, function(item) { return [ item.category, item.name ]; }); 

此息率:

{ 
    b: { 
     foo: [ 
      { 
       id: 6 
       name: foo 
       category: b 
      } 
     ] 
     beer: [ 
      { 
       id: 5 
       name: beer 
       category: b 
      } 
     ] 
     bar: [ 
      { 
       id: 3 
       name: bar 
       category: b 
      } 
     ] 
    } 
    a: { 
     free: [ 
      { 
       id: 4 
       name: free 
       category: a 
      } 
     ] 
     foo: [ 
      { 
       id: 2 
       name: foo 
       category: a 
      } 
      { 
       id: 1 
       name: foo 
       category: a 
      } 
     ] 
    } 
} 

無論如何,希望這可以幫助別人。

+3

爲腳本+1!我試圖自己實現它,但沒有運氣:P ...在我看來,它太神祕了,但它起作用,無論如何,我用「arr」替換了「items」以使其正常工作。謝謝;) – daveoncode 2011-08-03 12:39:44