Array Manipulation - Unwrap Objects In Xano (@nocodebackend)

Ray Deck shows how to flatten an array of wrapped objects in Xano using a foreach loop and reducer. He extracts nested objects via dot-path syntax, appends them to an output array, and demonstrates debugging to verify the transformation works. The same technique can overwrite the original variable with the unwrapped result.
Chapters
All right, let's take problem today from the StateChange community where we have a manipulation we're trying to do here of let's take an object that has, or an array, right? That has this, you know, has the square brackets here and then basically is double nested, right? So you can see you've got, you know, the array which has leader inside, one key called leader and the leader in turn has this big object inside. And it seems clear that what's meant to happen is that this instead should just be an array that has an object. The object should have the properties ID, first name, surname, profile photo. And then next one should have just ID, first name, surname, profile photo. So, uh, what we basically need to do is unwrap this internal, um, uh, object and lift it so it just goes directly with the array itself, basically taking this leader part out of the middle. So let's talk about how we might do that here. And I do this, I made a little bit of a function and you don't need to, but this is what I did for illustration purposes. We're gonna start with whatever the array is and the array would basically be the equivalent of whatever this were looking like. And I made a little sample of that by saying let's take an array which will have, you know, be an array of objects, right? Each object only has an element a, although you could always call it leader. And each one of those has X and Y. And basically the correct answer we want to come back with is going to be an array that just contains X, Y, 1, 2, and another one which has the members X, Y, 3, 4 without having these A's in the middle. So doing this kind of array surgery can be a little bit involved. This essentially what's called a reducer pattern. Could be done as a mapping pattern, but in Xano they're kind of the same thing. You're going to create a variable, you're going to collect to make the new array, and I call this one output. I then iterate over this original array, and you do that with a foreach loop, passing it the original array, and then the iterator variable itself is gonna be called item. So item is gonna be this object like you see here, it'll have one member called A and the A in turn will have members X and Y, whatever. The, and then inside here we're looking to pick off the contents of A. So I do that by creating something I'm calling child and saying it to item.A. And in Xano, that dot syntax means take the child of whatever the object was. In this case, this is child over this whole thing. At which, and take the member at path, which is identified as A, which is here, and then, and then pull its contents. So what item.a should do is produce all this stuff. You know what? Let's just confirm that it does that. So we will use one of my favorite functions, stop and debug, and we will say, let's make it into, What am I looking for here? I'm looking for whatever child to be, right? Okay. So if I did this correctly, the child should be the 1 and the 2 here. So let's find out. Do run debug. Run. Right, so now I get the object, which is the 1 and the 2, right? So it's unwrapped from the A. Having unwrapped it, we'll comment that out for a second. Having unwrapped it, we now want to add it to the end of our output array. And by doing this, we clean, clean, clean, clean, clean. And our input might have been the original array that has a wrapping, but the output just gets this new version, not the A version, but just the XY version added back to the output list. And then at the end, I'm returning this. So what should happen is I should be, you know, it should iterate through, it should grab the first element, which is A with the X and the Y, grab just the X and the Y part. Add the X and Y part to the output variable, move around, grab the next A, grab the X and the Y, take the X and the Y part, put it into the output, and then we're done. So the correct result should be, I think, parallel to what, you know, the original question is looking for, which is, we should have A, or rather we should just have two objects in here, which contain the XY12 and the XY34. Let's see how it works. Boom, there we go. Let's see if we can extend this a bit. What if this thing didn't just contain X and Y? What if it also contained Z of 5? Right, make sure there's a comma here so it's properly formatted. I do this again, and you see it picks up the Z5 from the first one because it'll pick up everything that's underneath the A. In fact, even if I were to say, you know, Uh, you know, Q, 9, uh, R, you know, 10. It could pick up like, you know, further complicated structures, right? But what it's done is the core unwrapping job that we needed it to do. We needed it to unwrap, uh, the A's and put in just whatever the children were. And this technique of setting up an output variable and then looping over it and then filling in the output variable. Is my preferred way of doing this. Now, if in the event you wanted to be like just editing whatever the original variable was, first you can't do that with an input. So we would need to instead use it as a variable so that we could go to, you know, data manipulation and create a variable. And I'm going to call the variable, we'll call it r just like what the input is. And the value is going to be whatever the input r is. All right. And actually doesn't very matter what the order of these two things is. And then we could loop over the variable r, right? So remember, it doesn't have to have come from an input, it could just be from a variable and that's fine. And then instead, and then we can, and then just like before, we're just gonna update output. But then what we can do at the end, which is kind of cool, is we can do data manipulation, we can do update variable, and we can say that we want to set var to whatever output is, right? That way at the end, we'll have edited the array variable itself based on this, you know, the intermediate work that we have done here. In fact, this down below, right? So basically we'll have looped, looped, looped, looped, and then update the variable. There are other, you know, more specific surgical ways of doing this where we could be looking at each position and like be editing the array variable in place. Again, only if it's a variable, doesn't work for inputs. But I think that those more fancy techniques can wait for another video. And I suspect, I hope at least, that this will help, you know, the original questioner and anybody else who's looking at this have a sense of how we can do a little bit of ray surgery and unwrap your objects in Xano.










