0

我有两个 json 数据

var orders = [
   {seller: "Test1", sell_count: 5, sell_amount: 1500},
   {seller: "Test2", sell_count: 2, sell_amount: 7500}
];
var views = [
   {seller: "Test1", pc_view: 123, mobile_view: 87},
   {seller: "Test2", pc_view:56, mobile_view:23},
   {seller: "Test3", pc_view: 13, mobile_view:7}
];

使用 linq.js 如何加入如下

result = [
    {seller: "Test1", view_count : 210, sell_count: 5, sell_amount: 15000},
    {seller: "Test2", view_count : 79, sell_count: 2, sell_amount: 7500},
    {seller: "Test3", view_count : 20, sell_count: 0, sell_amount: 0}
]

请帮我!

4

1 回答 1

0

没有内置的方法来执行完整的外部联接,因此您必须复制它。有很多方法可以实现,这里有一个。

var result = Enumerable.From(orders).Concat(views).Select("$.seller").Distinct().OrderBy()
    .Let(ks => {
        var os = Enumerable.From(orders).ToDictionary("$.seller");
        var vs = Enumerable.From(views).ToDictionary("$.seller");
        return ks.Select(k => {
            var o = os.Get(k) || {};
            var v = vs.Get(k) || {};
            return {
                seller: o.seller || v.seller,
                view_count: v.pc_view + v.mobile_view || 0,
                sell_count: o.sell_count || 0,
                sell_amount: o.sell_amount || 0
            };
        });
    });
于 2019-02-14T18:38:29.970 回答