Show Collection Data That Has ALL Attributes That Occur in Answer

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)…

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:

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.

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

Thanks @Jeff_Hager
Still doesn’t work though :confused:

So your last line of code looks like this?

return findMatchingIds(p1, p2, p3);
1 Like

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

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

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.

2 Likes

You, my friend, are the end-boss.

That’s it! Thanks a ton @Jeff_Hager for your help :saluting_face: :slight_smile:

1 Like

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