How can I show/hide based on a value being within an array? (AC - eg. client side)

I have a json field containing some values:

[3,7,12]

I want to show or hide a div based on a value being in that list. Server-side I would use IN in the query but I just want a simple solution to do it client-slide.

I can’t find the necessary option within the attributes.

Hey Jon,

Have you tried yourArray.contains(‘thevalue’)?

1 Like

Genius! That’s exactly what I needed. This doesn’t appear in the options within the UI so hadn’t tried it.

I’ll post a feature request so it’s added.

Cheers. You’re a star.

1 Like

Weird brother…
Conditional->Contains

Hmm, not there for me:

Screenshot 2023-12-06 at 18.07.23

That’s my array:

"pageRoles_Arr": [
        "U",
        "S",
        "M",
        "A"
    ]

What’s yours array?

Change the variable type from object to array

Hmm, it’s a json field in the database which contains the array. I don’t get the option to change its type.

yourArray.split(’,’).contains(searchValue)

Sorry to interrupt here, but I have a question about contains.
Let’s say I have [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Array.contains(‘1’) retrieves 1,10,11 or I’m wrong?

Works like SC?


That’s a good question. I had assumed it wasn’t treating the array as a single string and looking for a value within it but, instead, split it into virtual records and did a search on those records for the value equalling it. But I now wonder if I’m wrong.

I’m hoping I’m not, though, as that’s how it should really work.

Hey Jon,
I apologize brother…
The Json field coming from a database looks like “15,16,18”.
That means that the first item is "15 , the second is 16 and the last item is 18"

In my test before I answer to you, I just searched (randomly) for the middle one and it was it was correct… But if I search for the first or the last item it will not match… Because "15 is not equal to 15

So, my workaround on this is to replace first the double quotes with '' and then the rest:
yourJsonField.replace(’"’, ‘’).split(’,’).contains(theSearchValue)

Be aware, you always search for strings… So, if you check for 15 you have to say contains(15.toString())

And we don’t have problem on retrieving part of an item in an array… (1 instead of 11)
It will match the exact value with the values between the commas

1 Like

That’s super helpful. Thanks @famousmag.

1 Like