Marco
Marco

Reputation: 83

Slate - creating a TypeScript Function to filter and return an object with max property value

take a look at NEW RELATED QUESTION:

I want to filter an object set to retrieve the largest number of a column.

i do not know how to solve it. i try with max etc. But i think it is a skill problem. Here is my code so far:

     @Function()
    public async largestNumber(): Promise<Long> {
        const objResult = Objects.search()
                .dataMain()
                .filter(data_column => data_column.lngPlanningNumber.range. )
                .gt(100)
                .take(1);
                
        return objResult.max();
    }

This function returns objects with NULL values for the lngPlanningNumber property, which I would like to filter out.

UPDATE:

Property 'isNotNull' does not exist on type 'INumericPropertyFilter'.

for

.filter(data_column => data_column.lngPlanningNumber.isNotNull()) // filter out NULL values 

Property 'take' does not exist on type 'Promise<number | null>'.

for

.take(1);

NEW RELATED QUESTION: my code

    @Function()
    @Edits(XYZ)
    public async fctLargestNumber(): Promise<XYZ[]> {
        const maxObject = Objects.search()
                .xYZ()
                // .groupBy(e => e.lngPlanningNumber.topValues())
                // .segmentBy(e => e.lngPlanningNumber.topValues())
                // .filter(data_column => data_column.lngPlanningNumber.byIRanges({ min: 100000, max: 999999 }))
                .orderBy(data_column => data_column.lngPlanningNumber.desc())
                .takeAsync(1)
                //.valueOf();         
        return maxObject;

no i recieve an output like this:

[
{"typeId":"my-collection","primaryKey":{"id_pk":"ee1b1ac1-008b-479b-a748-01e8702927c9"}}
]

The question is now, how can i receive my result.

The Promise makes me curious. Thank you

Upvotes: 2

Views: 780

Answers (3)

Marco
Marco

Reputation: 83

@Function()
public async largestNumber(): Promise<DataMain[]> {
    const maxObject = Objects.search()
            .dataMain()
            .orderBy(data_column => data_column.lngPlanningNumber.desc())
            .takeAsync(1);         
    return maxObject;
}

Upvotes: 0

Kabiswa Davis
Kabiswa Davis

Reputation: 21

You can achieve this by filtering the object set, order by descending the desired column and then take(1) or takeAsync(1) .

  @Function()
public async largestObject(): Promise<DataMain[]> {
    return Objects.search()
            .dataMain()
            .filter(data_column => data_column.lngPlanningNumber.isNotNull())
            ..orderBy(data_column => data_column.lngPlanningNumber.desc())
            . takeAsync(1);
}

Upvotes: 0

fmsf
fmsf

Reputation: 37137

I can confirm that for numeric properties you'll only find the filters exactMatch and range. Nulls should get automatically removed when using these filters.

You can always try to do a range that gets all your values:

const flight: ExampleDataFlight[] = Objects.search().exampleDataFlight()
            .filter(row => row.taxiOut.range().gte(-99999).lte(99999))
            .all();

Regarding the take(1) take property requires ordering to show up. Here is an example where I randomly used airtime to force the orderBy, if you only want to do take(1) you can land any property here.

const flight: ExampleDataFlight[] = Objects.search().exampleDataFlight()
            .filter(row => row.taxiOut.range().gte(-99999).lte(99999))
            .orderBy(row => row.airTime.asc())
            .take(1);

Upvotes: 0

Related Questions