0

我需要基于两个对象创建一个数据透视表。为此,我的第一步是连接两个对象,然后通过打字稿函数获得一个组合对象。从该对象中,我将 groupBy 并执行一些聚合函数(sum,min)并获取所需的列。请建议我这样做的方法。

我试过下面的代码:

public async test(p1: ObjectSet<objecta>,c1: ObjectSet<objectb>): Promise<ObjectSet<objectc> {
         
    const [results1, results2]= await Promise.all([
    #three dimensional aggregation
    p1
        .groupBy(a1 => a1.year.topValues())
        .segmentBy(a1 => a1.code.byFixedWidth(1))
        .sum(a1=>a1.amt),
    c1
        .groupBy(a1=>a1.yr.topValues())
        .segmentBy(a1 => a1.code.byFixedWidth(1))
        .sum(a1 => a1.FullAmt),
    ]);

    # results3 is three dimensional aggregation how to convert it to object set?
    const results3 = results1.map(itm => ({
        ...results2.find((item) => (item.yr === itm.yr) && item),
        ...itm
    }));
}
4

1 回答 1

0

I don't believe this is really supported. Why do you need to return an Object set?

Would it work if you just return a custom structure?

Here's an example written straight into SO, so it may not work verbatim, but it gives you an idea :) i.e.:

interface IObjectCStructure { 
   foo: string
}

public async test(p1: ObjectSet<objecta>,c1: ObjectSet<objectb>): Promise<Array<IObjectCStructure>> {
    ...

    const results3: Array<IObjectCStructure>> = [];
    
    // I doubt you need a n^2 search
    // this will time you out if the lists are too big.
    // I'm just keeping this here so I don't change your code too much,
    // please consider using sets instead of nesting loops.
    results1.forEach(a => 
        results2.find(b => (a.id === b.id) && a)
                .forEach(item => results.push({
                     foo: item.some_field_you_want
                }));
        
    return results;
}
于 2021-11-24T17:19:59.733 回答