Counting Objects in Array for Crowdsourced Categorization

Hey Glide Community,

I’m working on an application that enables “giving circles” for a community, simulating a hybrid vehicle that borrows from the dynamics of both an endowment fund and a directed trust. The goal is to create a perpetual “community-directed giving pool” that directs gifts to beneficiaries identified by contributors.

One of the features of this tool will enable contributors to identify and cluster potential grant/gift recipients into categories of benefit (environmental, scientific, charitable, etc.)

On to the question…

I have a form that allows contributors to categorize a beneficiary (somewhat similar to creating a review).

Having created a relational column for this categorization system, I’m able to view the “related categories” of a given beneficiary.

On to the question…

Right now, the green banner (at the top) with focus category is set manually. Is there a way to identify which entries appear the most in an array, and set a column contents to a value in the entry? Using these submissions, I want to be able to identify the highest aligned category and have a column that changes when one category may overtake another.

This is what the back-end looks like. You can see that the beneficiary on the bottom row has two categorizations as “environmental” so I would want the “category” column to match the output of that function.

As always, any help is appreciated!

Here’s my draft.

First, you convert your array into a comma-delimited list by using a joined list column, with the delimiter being a single comma (no extra spaces).

Then, you add a JavaScript column and use this code.

function mode(array)
{
    if(array.length == 0)
        return null;
    var modeMap = {};
    var maxEl = array[0], maxCount = 1;
    for(var i = 0; i < array.length; i++)
    {
        var el = array[i];
        if(modeMap[el] == null)
            modeMap[el] = 1;
        else
            modeMap[el]++;  
        if(modeMap[el] > maxCount)
        {
            maxEl = el;
            maxCount = modeMap[el];
        }
    }
    return maxEl;
}

let arr = p1.split(",");
return mode(arr)

Adapted from:

5 Likes

Wow, that worked like charm, @ThinhDinh!

You have my sincerest thanks.

1 Like

It doesn’t handle ties though (say there are 2 or more categories with the same max occurrence). If you want that to be handled we can change the code a bit.

1 Like

That’s also helpful to know. I’m expecting this feature to be used on a daily basis, so I don’t expect ties to occur too often, but it would be something to address in the future. Would the code error, show two values, or just keep one - likely the preceding value?

Thinh, you saved me many hours of time! Thank you! :raised_hands:

Generally, I would be interested in getting your feedback and advisory eye on the app and our architecture. Do you mind if I reach out over PM to discuss potential support?

It will always show the first one among the ties.

I may not have enough time to support you, so if you have any questions please post it publicly in this forum, many people will be around to help.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.