Filtering an object with multiple criteria in Xano

Ray Deck shows how to filter objects in Xano by matching their keys against a list of desired keys. The solution loops through the key list and uses find first element to locate matching objects, building a new array in the order of the keys rather than the original objects.
Chapters
Hey, so one of the more challenging things to do is to take a list of objects and say that we want to filter them based on multiple potential filters we want them to match up on, right? Like we want them only where the, you know, like everything where the key is going to be a certain value, except we want to have multiple values and we want to do that in a somewhat performant way. So if we were to sort of look at this in terms of what inputs would look like for that, we could have like a list of objects with, you know, they're objects each which are just gonna be little key-value pairs, right? K of A, value of X, K of C, value of Y, K of D, value of Z. And maybe there are a bunch of keys we want to be able to map it with. And this is a generalized problem, right? So like this could be any number of types of data that we might want to work with. So we might say there's, we want to identify a certain key and we're going to say that if that key is in one of these, that we would like it to filter just to that particular list. Now in code, this is something that's actually relatively straightforward to do through like a generalized array function. This is part of functional programming. And to do this in sort of a no-code environment like Xano, we do it a little bit more from first principles. So the idea is we're not actually gonna modify this existing array 'cause it's gonna be an input to the function. We're going to create a new one that will only have our filtered list. Now to do this, we're going to need a few things. We're gonna need to know what are the objects, what are the keys and what's the, you know, path to that key. And we're going to create a new list, right, of objects. We'll actually contain all these guys. That's going to be the, that's the output here. And then what I usually do is I loop over the filtered list, right? The list of good keys. That's usually a shorter list. That's gonna make the thing run a little bit faster. And so I'm gonna say I'm gonna loop over each of my keys and I'm just gonna call that key for a second. And that way if I were to, you know, quickly give this a little, you know, utility and stop and debug, always my favorite function, I could say that the current key that I'm looking at, and if I were just to run that guy for a second, run, right? It'll tell me that the first key is A. And now what I want to do is look through the objects to find the one that has a K of A, right? Just like the thing I see here. So how do we do that? We're going to, I'm just gonna comment that out and we're going to go down to the next part of this, which is this find first element bit. This is really the key to the whole thing, right? 'Cause I'm going to say I'm gonna look over my list of objects and I'm going to ask whether, and this has to do with the way these sort of iterator array functions work. There's a magic variable called this that's referring to the current item in the array. So it's gonna look at each object of key-value pairs like the ax and the cy and the dz. And this one's gonna refer to the ax object first and it's going to pull out the member of the object that matches the key name, right? 'Cause we need to figure out which of these are the key 'cause from the computer's point of view, it has no idea what an actual key is. It just knows there are two members to it, k and v. And so we're going to tell it that we want to use K because that was the input that we got from the variable that we got for the function. Now we're going to ask, does that value stored to that key name K, in this case it's gonna be A, we're going to ask, does that match the current member of the keys list that we're looking at? Does it match A? And then it's gonna look at the next part of the object. It's gonna ask, okay, does CY match A? And match A, and that's what we know. First one will be the answer will be yes, second one the answer will be no, third one will be the answer will be no. But that's okay because all we're trying to do is ask, does it have an element that matches? And it finds the first element and stops. So wanna know whether there's any element in the, whether basically the, whether this key has a matching element in the object array. And if it does, I'm going to pull this match with the matching element and I'm going to put it into output, right? So I'm going to say first I'm going to try to find it and then if it matches, I'm going to add it to my output array. And I just do that for each of my keys for A and B in the case of the, you know, little example I'm doing here. So it's going to look at A and look at B and say, hey, A, Do you have an object that matches A? I do have an object that matches A. That's what this guy finds. And then, and then if it found it, it adds it to output. Now second, it's gonna look at B and when B looks at this list of objects, it doesn't find anything, which means that this match returns null. And if it is null, well then it won't add it to the array. That way we don't get nulls inside the array. The end result of this should be that this will give me a list of filtered, of just filtered objects, kv. So I would expect that the, given these 3 objects and given these 2 keys, I should get 1 result, which is where the key is equal to A. Try running it. That's exactly what I get. Now, if I were to say extend this list to include C, I get A and C. If I were to change it, I want to do D first. Note that also that the output of this is going to be based on the list of keys as opposed to being based on the list of the original objects. You of course can like, you know, resort this or whatever you want to do with the data afterwards. I think this gives you a starting point for how to start to manipulate this information in Xano. Xano provides you with lots of primitives to make it easy to reason your way through the whole thing. But of course there are a few different moving parts and hopefully you found this little tutorial helpful for identifying what those moving parts are in order to be able to do your more complicated matching in Xano.










