# How to count occurrences for each values in an array?

**URL:** <https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708>\
**Category:** Ask for Help\
**Created:** [March 3, 2023, 2:41am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708 "2023-03-03T02:41:59Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikec](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mikec/32/53291_2.png) [@mikec](https://community.glideapps.com/u/mikec)\
**Post date:** [March 3, 2023, 2:41am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/1 "2023-03-03T02:41:59Z")

</div>

Hey folks, I’m stuck with this one and could really use some help. I have 3 tables set up:

- **Brands** contain all infos for brands, with relation to **Reviews**
- **Reviews** contains user reviews of different brands, and every row contains Tags for each brand.
- **Tags** is a helper table with all the tags

I’m trying to count how many times each Tag have show up for each brand:

 ![Screen Shot 2023-03-02 at 6.16.28 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/0/4/042320931a1d454f512c44f2718bd11819605216.png)

In this example, the count should be:  
Bad Packaging 1  
Ethically Sourced 1  
Tasty 😋 2  
High Sell-Through 1

I hope to generate something like the following, and plot the infos in a radial chart  
 ![Screen Shot 2023-03-02 at 6.41.20 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/f/f/ffbec7e7dde18b3b0f31ec659882d3e98c00b218.png)

Thanks so much in advance!

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 3, 2023, 3:40am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/2 "2023-03-03T03:40:11Z")

</div>

Would you mind adding screen shots of all 3 tables?  
I think the one you already added is from your Reviews table? 🤔

---

<div class="post-metadata">

**Author:** ![mikec](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mikec/32/53291_2.png) [@mikec](https://community.glideapps.com/u/mikec)\
**Post date:** [March 3, 2023, 4:03am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/3 "2023-03-03T04:03:17Z")

</div>

Hey Darren! Sure thing!

 ![brands](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/6/7/676a8e73ab4e11076289f2f2d2c275e7e68e0d56.png)  
 ![tags](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/7/1/71848c8da53c95f902a6a4164463f951b797e04e.png)

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 3, 2023, 5:27am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/4 "2023-03-03T05:27:00Z")

</div>

Okay, cool.  
Another question - when would you want this summary to be generated?  
For example, is it something you would expect to have available when you view a Brand Details screen?

Also, the joined\_Tags column shown in your Brand Database table - is that via a relation to your Reviews table?

Just want to make sure I have a clear picture of your setup and desired flow.

---

<div class="post-metadata">

**Author:** ![mikec](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mikec/32/53291_2.png) [@mikec](https://community.glideapps.com/u/mikec)\
**Post date:** [March 3, 2023, 6:52am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/5 "2023-03-03T06:52:51Z")

</div>

1. Yea, ideally tags and counts can be displayed when people visit individual brand’s page.
2. Correct, it’s pulling from the reviews table.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 3, 2023, 7:30am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/6 "2023-03-03T07:30:22Z")

</div>

Okay, take a look at this:

---

<div class="post-metadata">

**Author:** ![mikec](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mikec/32/53291_2.png) [@mikec](https://community.glideapps.com/u/mikec)\
**Post date:** [March 4, 2023, 2:35am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/7 "2023-03-04T02:35:30Z")

</div>

Wow, thank you Darren! Amazing solve as always!

---

<div class="post-metadata">

**Author:** ![mikec](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mikec/32/53291_2.png) [@mikec](https://community.glideapps.com/u/mikec)\
**Post date:** [March 4, 2023, 2:49am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/8 "2023-03-04T02:49:36Z")

</div>

Curious, would this be possible to solve with either Experimental code or Javascript?

Counting occurrences in an array feels like such a simple task, had no idea can get this complicated. 😂

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 4, 2023, 3:22am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/9 "2023-03-04T03:22:07Z")

</div>

Yeah, it would be possible.  
If I was to do it using JavaScript I’d still take the same approach, but I’d replace about two-thirds of those columns in the Tags table with a single JavaScript column. Essentially, once you have the Joined List of tags, you can pass that to a JavaScript column and do all the work there.

So the input to the JavaScript column would be the `jlTags` column, and the output would be the same as the `ifTagSummary` column.

---

<div class="post-metadata">

**Author:** ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)\
**Post date:** [March 4, 2023, 12:18pm UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/10 "2023-03-04T12:18:53Z")

</div>

Well,

I usually take this way to solve this requirement (via JS):

> [@1234 Count duplicate elements in an array or list](https://community.glideapps.com/t/count-duplicate-elements-in-an-array-or-list/41313):
>
> Hola! This is not a feature request per se, instead, it is an easy solution to know how many times an element is in an array or list to use it later and create a ranking, a sorted list, etc. Glide has many array plugins but I don’t know why something like it is missing when I needed it (it’s the reason I created this small code). Maybe the Duplicated Items plugin is closest to get it but it’s not enough. Well, the case could be this: [image] and in order to know how many times a…

Saludos!

---

<div class="post-metadata">

**Author:** ![mikec](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mikec/32/53291_2.png) [@mikec](https://community.glideapps.com/u/mikec)\
**Post date:** [March 4, 2023, 4:55pm UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/11 "2023-03-04T16:55:13Z")

</div>

Hey gvalero! Great seeing you here, I actually started with your solution few days ago, I was able to extract counts from my Tags table using relation to Reviews table and a joined list.  
But couldn’t quite get the js code to work inside Brands table which is what I wanted. I hope to have individual tag counts for each brand and display them.

Any chance you could help adapt the code?

---

<div class="post-metadata">

**Author:** ![mikec](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mikec/32/53291_2.png) [@mikec](https://community.glideapps.com/u/mikec)\
**Post date:** [March 4, 2023, 4:59pm UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/12 "2023-03-04T16:59:23Z")

</div>

Hey @gvalero @Darren_Murphy , also I noticed that inside a collection, if i use Tags as filter, it actually does what I hope. So the function is there, just not sure how to extract it.

![filter by Tags](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/5/8/5892c0c8202258b2a61c65ad3f744bafb018ef57.png)

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [March 5, 2023, 12:05am UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/13 "2023-03-05T00:05:01Z")

</div>

That is natively handled by Glide and you don’t have that info available to be extracted.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 5, 2023, 2:46pm UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/14 "2023-03-05T14:46:59Z")

</div>

@mikec here is a JavaScript option that I alluded to earlier in the thread:

> [@Darren\_Murphy](#):
>
> If I was to do it using JavaScript I’d still take the same approach, but I’d replace about two-thirds of those columns in the Tags table with a single JavaScript column. Essentially, once you have the Joined List of tags, you can pass that to a JavaScript column and do all the work there.
> 
> So the input to the JavaScript column would be the `jlTags` column, and the output would be the same as the `ifTagSummary` column.

```auto
let arr = p1.split(/,\s?/);
return arr.filter(x => x === p2).length;

```

 ![CleanShot 2023-03-05 at 22.42.34@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/2/3/239e3ce30b272bae1f1c418914dbc43875712dd5.jpeg)

Note:

- this is done in the Tags table, not in the Brands table. It’s much easier to do it this way.
- a regular expression is used in the split. This handles the case where there is a space after the comma in the joined list.

---

<div class="post-metadata">

**Author:** ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)\
**Post date:** [March 5, 2023, 5:04pm UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/15 "2023-03-05T17:04:53Z")

</div>

> [@mikec](#):
>
> So the function is there, just not sure how to extract it.

Hola!

Sorry for my late reply, but I see that Darren already found the missing piece you needed.

Saludos!

---

<div class="post-metadata">

**Author:** ![mikec](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mikec/32/53291_2.png) [@mikec](https://community.glideapps.com/u/mikec)\
**Post date:** [March 5, 2023, 8:59pm UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/16 "2023-03-05T20:59:02Z")

</div>

Thanks @Darren_Murphy, works brilliantly! Appreciate the help.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [March 6, 2023, 8:59pm UTC](https://community.glideapps.com/t/how-to-count-occurrences-for-each-values-in-an-array/58708/17 "2023-03-06T20:59:33Z")

</div>

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