How to do this in no code? Find if an array contains another array

I have an array of values (let’s call it array 1). I have another array of values (array 2).

Example:

Array 1: [1,2,3,4,5]
Array 2: [1,4,5]

How do I check if array 1 contains all the values of array 2 to return true or false, using no code?

In this case it will return true as Array 1 contains all values of Array 2.

I was thinking use the “Contains” filter but that only applies if Array 2 is a single value and not a list.

I would create a boolean variable - “doesContain” set to false

Then do a for each loop on array 1

If Array 2 does NOT contain (“contains” filter using item from loop) then RETURN w response of doesContain (breaks out and ends loop)

loop completes if Array 2 DOES contain - after the loop is complete you change variable doesContain to True - and return that as a response.

I’d say you could make this into a formula yourself.

return subArray.every(element => mainArray.includes(element))

and set parameters mainArray and subArray in your formula configuration like such

I’m wondering there is not a no-code formula for this.

1 Like

Love this.

1 Like

Also a nice article about simple useful array manipulation that can be included as formulas: Array intersection, difference, and union in ES6 | by Alvaro Saburido | Medium

2 Likes

Hey Broberto! So I have created the formula exactly like you suggested in the “Formulas” section:

Now outside of the Formulas section in my app, I want to see if array 1 contains array 2 how do I do it?

For example, I have this text that I want to display if the subArray is within the mainArray:

I added the formula but don’t see a way to tell the formula what’s my mainArray and what’s my subArray?

First of all, you’re not switching the Parameter type to Array, you’re using Text instead. I had the exact same issue haha.

When you hover on the formula, it should show you the order. You basically just need to pass the parameters in order, as you would in any other formula in your case, you would first pass the array that IS CONTAINED and secondly pass the array that CONTAINS IT

sorry for the caps, but bold seems to not work on the forum for some reason. @Joyce is there any specific reason why markup doesn’t seem to work?

No idea. It seems to be a Discourse bug. Bold used to work but doesn’t anymore

Hi @raelyn :wave:

Not sure if you caught this but in your first screenshot you wrote the return statement twice so your formula won’t work

1 Like

Oh!! Right :grin: