How to join two objects and return object set as ouput in palantir through typescript functions

592 Views Asked by At

I need to create a pivot table based on two objects. For that my first step is to join two objects and then get one combined object through typescript functions. From that object I will groupBy and perform some aggregate functions(sum, min) and take needed columns. please suggest me ways to do this.

I have tried below code:

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

There are 1 best solutions below

0
On

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