建立在C#中的javascript中嵌套的对象像GROUPBY [英] create nested objects in javascript like groupby in C#

查看:165
本文介绍了建立在C#中的javascript中嵌套的对象像GROUPBY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?
一个通用的解决方案,??

Is taking a flat list and converting it into a nested object possible in Javascript? A generic solution that is??

推荐答案

是的。

您可以围绕在JavaScript中传递的功能,这将服务于相同的目的的C#代码的lambda表达式。就拿使用jQuery的每为例:

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

这篇关于建立在C#中的javascript中嵌套的对象像GROUPBY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆