create nested objects in javascript like groupby in C#

后端 未结 2 1323
感动是毒
感动是毒 2021-02-10 01:25
IList Customers =
            flat.GroupBy(cust => new { cust.ReferenceNumber, cust.Name, cust.Address })
                .Select(c => new Customer         


        
2条回答
  •  抹茶落季
    2021-02-10 02:04

    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

提交回复
热议问题