# Show Collection Data That Has ALL Attributes That Occur in Answer

**URL:** https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952
**Category:** Ask for Help
**Created:** [November 19, 2023, 3:56pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952 "2023-11-19T15:56:14Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Raphael\_Aebersold](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/raphael_aebersold/32/64167_2.png) [@Raphael\_Aebersold](https://community.glideapps.com/u/Raphael_Aebersold)
#### Post date: [November 19, 2023, 3:56pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/1 "2023-11-19T15:56:14Z")

</div>

Hi all,

This is a tricky one and I’m trying to be as specific as possible.

I would want to filter a collection (using the built in “filter data” function) according to an answer from chatGPT.

The answer is a JSON:  
{  
“marketing\_methods”: [  
“SEO”,  
“Social Media Ads (Facebook / Meta Ads, Snapchat Ads, TikTok Ads)”,  
“Influencer Marketing”,  
“Content Marketing”,  
“Event Marketing”  
]  
}

Now each entry in the collection has a comma separated list of attributes that may or may not include any of the items in the above JSON.

Also, these attributes occur in random order.

Now my question is: How can I filter out all entries in the collection that include all of the attributes in the JSON?

What I’ve tried so far:

- “Collection entry includes JSON” =\> Doesn’t work since its looking for the whole JSON instead of the items
- Query JSON and create an array, then look if separated values of collection includes values from array =\> Doesn’t work since “includes” does not work with computed columns
- separated list contains values from array
- array items contain array

Do you have any idea on how this could be solved in a neat fashion? I would know how to do it in regular code - its a simple “match” and if all matches are TRUE, then include collection item in list. But here it is a little weird it seems (or i don’t see the simple solution)…

---

<div class="post-metadata">

### Author: ![Raphael\_Aebersold](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/raphael_aebersold/32/64167_2.png) [@Raphael\_Aebersold](https://community.glideapps.com/u/Raphael_Aebersold)
#### Post date: [November 19, 2023, 5:48pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/2 "2023-11-19T17:48:13Z")

</div>

Okay, I’ve found a solution with implementing code and the code also works. However, the output is not returned to the cell in the database.

Do you know of any bug that’s currently active or is it sth else?

this is the code:

```auto
function findMatchingIds(termsStr, idsStr, itemsStr) {
    // Parse the JSON strings into objects
    const termsObj = JSON.parse(termsStr);
    const idsObj = JSON.parse(idsStr);
    const itemsObj = JSON.parse(itemsStr);

    // Function to replace non-breaking spaces with regular spaces
    function normalizeSpace(str) {
        return str.replace(/\u00A0/g, ' ');
    }

    const matchedIds = [];

    // Iterate over each item and its corresponding ID
    for (let i = 0; i < itemsObj.leistung.length; i++) {
        const item = normalizeSpace(itemsObj.leistung[i]);

        // Check if all search terms are in the item
        const allTermsMatch = termsObj.terms.every(term => 
            item.includes(normalizeSpace(term))
        );

        console.log(`Item ${i}:`, item); // Debug log
        console.log(`All terms match for item ${i}:`, allTermsMatch); // Debug log

        // If all terms match, add the corresponding ID to the matchedIds array
        if (allTermsMatch) {
            matchedIds.push(idsObj.verzeichnis_id[i]);
        }
    }

    console.log("Matched IDs:", matchedIds); // Debug log
    return matchedIds;
}

// Example usage with your JSON strings

findMatchingIds(p1, p2, p3)

```

=\> p1, p2 and p3 three are the variables (JSON Objects).

=\> the console log tells me that the array is generated correctly.

 ![Screenshot 2023-11-19 at 18.47.51](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/c/f/cfccb1b546d311c36dcffe93b82c15fd51a8a247.png)

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [November 19, 2023, 5:51pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/3 "2023-11-19T17:51:43Z")

</div>

> [@Raphael\_Aebersold](#):
>
> `findMatchingIds(p1, p2, p3)`

You are calling the function, but you are not returning the result from it. Add a return in front of this.

---

<div class="post-metadata">

### Author: ![Raphael\_Aebersold](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/raphael_aebersold/32/64167_2.png) [@Raphael\_Aebersold](https://community.glideapps.com/u/Raphael_Aebersold)
#### Post date: [November 19, 2023, 5:54pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/4 "2023-11-19T17:54:05Z")

</div>

Thanks @Jeff_Hager  
Still doesn’t work though 😕

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [November 19, 2023, 5:57pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/5 "2023-11-19T17:57:01Z")

</div>

So your last line of code looks like this?

```auto
return findMatchingIds(p1, p2, p3);

```

---

<div class="post-metadata">

### Author: ![Raphael\_Aebersold](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/raphael_aebersold/32/64167_2.png) [@Raphael\_Aebersold](https://community.glideapps.com/u/Raphael_Aebersold)
#### Post date: [November 19, 2023, 6:03pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/6 "2023-11-19T18:03:11Z")

</div>

![Screenshot 2023-11-19 at 19.02.47](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/1/5/156e48eec92a6a1c8d3961dd92386d91c1ea7c72.png)

Yes: `return findMatchingIds(p1, p2, p3);`

---

<div class="post-metadata">

### Author: ![Raphael\_Aebersold](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/raphael_aebersold/32/64167_2.png) [@Raphael\_Aebersold](https://community.glideapps.com/u/Raphael_Aebersold)
#### Post date: [November 19, 2023, 6:10pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/7 "2023-11-19T18:10:11Z")

</div>

I get this error in the console all the time: `Failed to load resource: the server responded with a status of 404 ()`

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [November 19, 2023, 6:12pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/8 "2023-11-19T18:12:16Z")

</div>

But you said the console log does show that MatchedIds does contain an array of values?

I’m thinking it might be the fact that you are trying to return an array when the JavaScript column can only return a string. You might have to change the return within the function to `return matchedIds.toString();` to convert the array to a string.

---

<div class="post-metadata">

### Author: ![Raphael\_Aebersold](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/raphael_aebersold/32/64167_2.png) [@Raphael\_Aebersold](https://community.glideapps.com/u/Raphael_Aebersold)
#### Post date: [November 19, 2023, 6:17pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/9 "2023-11-19T18:17:06Z")

</div>

You, my friend, are the end-boss.

That’s it! Thanks a ton @Jeff_Hager for your 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: [November 26, 2023, 6:17pm UTC](https://community.glideapps.com/t/show-collection-data-that-has-all-attributes-that-occur-in-answer/67952/10 "2023-11-26T18:17:54Z")

</div>

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