Counting and categorizing items in an array

I have a scenario where I have an array of items like this:
(apple, orange, carrot, apple, carrot, apple, potato)

I would like to categorize them into Fruits and Vegetables then count how many
I am looking to show results like this:
Fruits (4) and Vegetables (3)

Any ideas?

Yes, use group by option in the Array Aggregator module.

MSquare Support
Visit us here
Youtube Channel

@shawn, consider this your gift for the week that I hope you will grant back to someone in the community one day once you become a Make Jedi Master.

You Must Star Wars GIF by Regal

Your question is very loaded with a lot of stuff, but it is a perfect way to illustrate the power of Make aggregators. So actually demonstrating how to do this very useful object manipulation requires much more than just “use an array aggregator with a group by option”.

First of all you need to actually build an array that identifies the type of object an apple, carrot and potato are. This is easily done with the Parse JSON module which can convert a set of collections into one array.

Here’s the JSON that will make it easy for you to create the array that you can start aggregating in the next step.

[
{
"type":"fruit",
"object":"apple"
},
{
"type":"fruit",
"object":"orange"
},
{
"type":"vegetable",
"object":"carrot"
},
{
"type":"fruit",
"object":"apple"
},
{
"type":"vegetable",
"object":"carrot"
},
{
"type":"fruit",
"object":"apple"
},
{
"type":"vegetable",
"object":"potato"
}
]

Note that hopefully you will get your data input in a similar kind of structure because if not you’ll need to manipulate it to become something like this. Using ordinary array functions (eg add() ) it is impossible to create an array of collections like this. I wish someone would show me how add() can be used to create from scratch a complex array. I haven’t found a way other than using Parse JSON.

Next we will in fact use an array aggregator. Run the Parse JSON once to prime the output bundles, and you will have 7 of them for this particular array. The configuration will select the object to aggregate around, grouping by the type key.

The output bundles of the array aggregator will look like this, note the grouping around type now stored in the “IMTKEY” key, with an array of all the objects you grouped.

[
    {
        "__IMTKEY__": "fruit",
        "array": [
            {
                "object": "apple"
            },
            {
                "object": "orange"
            },
            {
                "object": "apple"
            },
            {
                "object": "apple"
            }
        ]
    },
    {
        "__IMTKEY__": "vegetable",
        "array": [
            {
                "object": "carrot"
            },
            {
                "object": "carrot"
            },
            {
                "object": "potato"
            }
        ]
    }
]

Well you’re not done yet, Young Padawan. The array aggregator itself won’t give you the count as you asked but a grouped by set of arrays.

Numeric Aggregator to the rescue. 2 Aggregators in a row, can you believe it?!?!

Here is where the magic of a numeric aggregator module show’s its strength:

image

Use the SUM aggregate function, and set the aggregation value to length(3.array). Length will calculate the length of the array and sum will count how many elements are in each array.

The output bundles from the numeric aggregator is:

[
    {
        "result": 4,
        "__IMTKEY__": "fruit"
    },
    {
        "result": 3,
        "__IMTKEY__": "vegetable"
    }
]

The length of each object is in the result key and you may now go on your merry way. Note that the numeric aggregator has output 2 bundles. Which means the next module will run once for each bundle.

You can use YET ANOTHER AGGREGATOR to aggregate both bundles into one output bundle:

Yielding this nifty result which even includes the total bonus number of object types in the __IMTAGGLENGTH__ key

[
    {
        "array": [
            {
                "result": 4,
                "__IMTKEY__": "fruit"
            },
            {
                "result": 3,
                "__IMTKEY__": "vegetable"
            }
        ],
        "__IMTAGGLENGTH__": 2
    }
]

Now that my friends is how you answer a question on the Make Community.

ps. Here’s the blueprint too.
Counting Array by Object Types.json (5.6 KB)

pps. For future reference it took me about 40 minutes to create, document and write up this response. A nontrivial response, hopefully a GPT-powered response will eventually generate.

Great work Alex - super useful answer. Thanks for your efforts.

1 Like

@alex.newpath what is “IMTKEY”. @Callinetic used it in this response - i assumed it was to do with the headers but now you have used it as well. (no rush to respond).

It’s just a key that make assigns for when it creates an array and needs to manufacture a key to store a value in. There are several of these IMT- values that appear in arrays that make has to create structures.

1 Like

Thanks for stepping in. I have adjusted my post and removed any contention there. I hope we can all contribute to the best of our abilities.

2 Likes

Absolutely. @alex.newpath

2 Likes

Awsome!!
Can i use this to create multiple records/rows in Airtable or Google sheets?
I mean: 1 row per group