create nested objects in javascript like groupby in C#

c#, group-by, javascript, object

Solution

Yes.

You can pass functions around in JavaScript, which would serve the same purpose as the lambda expressions in the C# code. Take the use of JQuery's "each" as an example:

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

You can also create nested objects quite easily:

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

Of course, you can do that dynamically, rather than all at once:

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

Then, to do what you're looking for, you'll need to use arrays:

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

Problem

``` 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() ``` Is taking a flat list and converting it into a nested object possible in Javascript? A generic solution that is??

Original source

Related problems