Finding intersecting arrays in Xano

Learn how to build a Xano function that checks if two arrays contain the same elements in any order. The function uses arraydiff twice to compare both directions, returning true only if no differences exist between them.
Chapters
You have an array A, you have an array B, and actually it's fairly straightforward to fix this. That you would say that, I think that the goal is if array A contains something that's not in array B, you want to return false. If B contains something that's not in A, you want to return false. You only want them to be the same if they are both containing the same elements. And I'm sort of assuming that you also care about sort, right? So you want to make sure that even if one has 1, 2, 3, and then one has 1, 3, 2, it would return true. So I whipped up a quick function called array compare that makes use of the array diff function built into Xano. You can set up a— wrapping things in functions is a great way to go. And you can create your inputs just by pressing the plus button next to the inputs. Oh, by the way, the way you create a function is by going over to the library over here on the left and creating a function just like I did here. So you can say plus, and then you can add another element. And the way to make it something in array coming in the door is to define it as whatever the base type is. In my case, it's anger, and I can call this C, and make sure you define as a list. That will force it to be an array. In fact, I don't want to have this next item, so I just say delete it and it goes away. What I would do is start with the presumption they're both the same. I create a variable called output and I initialize it as true. And then I set up conditionals. I set up conditionals by creating a function that's going to be a, what they call a data manipulation function, which is a loop, and I'm, oh, not a loop, excuse me, a conditional. It's setting up an if-then., and the if-then in this case is going to be based on this criteria, which is asking does argument B contain anything that is not in argument A? And I do that using a function called diff, and diff just takes a second argument as its, a second array as its argument, and will return whatever elements are in B that were not previously, that were not also in A, and that diff basically if there's anything in there then that will generate a new array, like a new element, and I can just check for that count. So basically if there's anything in B that's not in A, that count would be greater than 0, and in that event I would say that means that the output should be false, right? Because that means that the two arrays can't be the same. And then, you know, you got to look at the other way around too. So I set up a second conditional in the event that the first one test passed, and I say, all right, well then I'm just going to ask the same question in reverse. Does A contain anything that B doesn't have? And if that count is greater than 0, well then that must be a problem too. So now I've sort of surrounded the problem in the event that either of these are different from the other, i.e., they contain something the other one doesn't have, then I set the value to false, otherwise it remains true, and that's fantastic. So let's see if that whole thing works. I can go to Run and Debug, and I can say, well, if this one has 1, 2, 3, 4, and this one just has 1, 4, 2, well, 3 is in A and not in B, so it should fail, right? Indeed it does. If in fact they're the same and I want to say 1, 2, 4, and 0, 3, right now these are the same but they're in a different order, it returns true, which is supposed to, right? If on the other hand I have missing something here, like let's say this one has 1, 2, 4 and this one has 1, 4, 2, 3, i.e., this one has 3 and this one does not, I do run and it returns false. So it seems to handle all the situations we're looking for, and you do that just by making use of the array_diff function and a bit of nested if then-else logic.










