Looping in Xano (@nocodebackend): For Each, For, While, Continue and Break

In this tutorial Ray Deck walks viewers through Xano’s built‑in looping mechanisms for data manipulation. He begins by framing the need to iterate over records to transform or extract information, then introduces the three core loop types Xano supports: the foreach loop (which iterates over each element of a data set), the for loop (a numeric index‑based iterator), and the while loop (a condition‑driven iterator). For each loop type he demonstrates concrete use‑cases: using foreach to add calculated fields and perform reducer‑style aggregations, leveraging the for loop’s index to zip parallel arrays or reference items by position, and employing while loops for paginated API calls or retry logic where the number of iterations is unknown. Throughout, he shows how to control flow with continue and break, how to embed conditional logic, and how to safeguard against infinite loops with counters. Debugging utilities such as stopanddebug and debug_log are highlighted as essential tools for observing loop execution. The video concludes with guidance on when to choose each loop type and encourages viewers to experiment with combining loops and conditionals to build robust data‑processing pipelines in Xano.
Chapters
Hey, so something Xano is very good at is data manipulation. But in order for us to know how to manage that data, we need to know how we're going to iterate and loop over it in order to be able to do the things we really want to do. I've seen lots of people start with just like a one-liner that's going to do everything inside like a query all records type call. But the power is when we can take data and we can start to manipulate it by going over it. And there are 3 ways that we can do looping in Xano, and I think they're all really powerful, and we're going to be talking about those today. The, the first of those that is the most popular is called foreach, and the way we can find all these, by the way, is we can go to Data Manipulation and we can take a look at loops, and we will discover that there are a few things of particular interest here. One is the foreach loop, which is the most popular one we're going to get started with. There's a for loop that actually, instead of looping over data, lets you just loop over a set of numbers, and that can be really handy in specific use cases. We'll talk about that as well. And a while loop that, rather than setting a certain number iterations, operations are going to do, like based on the length of a list or a number between 1 and 10, just keeps on running as long as its condition is true. And this can be really handy for situations where you don't know how much you're going to get, like when we're getting paginated data will become a big use case. And then there are 3 other, you know, items in here, which have to do with what you can do inside the context of that foreach loop, including being able to say no, don't want to keep going, let's just break out of it. As well as being able to skip everything inside the current block and continue to the next step. But let's, let's talk about the 3 ways we can do loops, because I think for many people, they're not that intuitive. And when you see them, they can become super, super powerful. Let's start with foreach, most popular one. So foreach loop, we're going to loop over some source of data, and we're going to refer to it using item. So let's talk about what we would want to get. So let's just do data manipulation— no, sorry, database requests. And we'll get all the data from, I don't know, one of these, you know, JSON tests or something. Okay. And there's going to be— we're just gonna say we're gonna grab everything that's inside it. We'll say save there. And we will move that up here. And then we're going to loop over that data. Now, just quickly, let's set ourselves up using my favorite function, which is called stop and debug, which is a utility function. And stop and debug. And the value is just going to be whatever we got back from JSON test, that first table I have. And we'll do a run and debug. And you see now I've got data in it, right, which is basically every single record in the table, which is being represented as an object. And I might want to do things to each of those records or learn things from each of those records. But it's pretty common that you're gonna have multiple data, multiple responses come back, and you want to do something for each of them. So a thing we can do in here is we can say for each of those responses that we got from JSON test 1, we want to do a thing. And the, the way it will do that is we'll take the first record, second record, third record, and we'll just turn it into an object that usually might call item or record or whatever it is. And we can just look at that in isolation. So let's just show how that works for a second. I go in here, I say stop and debug the item. So notice how this is an array with the, the, you know, square brackets here and the curly brackets. So that means it's an array outside of objects inside. And those objects contain things like ID, created, etc. So when we're looking at the full JSON test one, we're going to look at that array, and you can see the multiple records that come back. And then if we want to look at just a single item within it, we should expect it just to be the object. So let's test that out. And let's confirm. And indeed, that's exactly what we get, we get just the one item. Now, this is great, because now we can, you know, maybe collect information about these items in here, or we can even modify them. So what if we wanted to add Well, let's just say here, let's just say we wanted to modify each of them to set to include a tomorrow field that will be equal to the created at plus a day. So we'll do add and we will do a data manipulation and we will do an update of the variable and the existing Variable is the item. And these we can say the items. You know, we're going to say it's equal to item tomorrow.. And by doing an update in this way, it will create the element with that field. Even though the field doesn't exist, it will get created as a result of running this, this update call. And we'll set a filter of item— sorry, of transform the timestamp from And sorry, not transform. This is going to be— oh yeah, right. So this will be plus 1 days. And we will then say that this is fine. So go to the next day. And the cool thing is this will now create something called tomorrow. Now, you'll notice right now in this object, there's nothing that's called tomorrow. So save this. And if I then do the stop and debug underneath it, right, of the item, you will see that it doesn't— sorry, tomorrow being set to now. All right, that was an error that I made. The crux of the error is you either can't reference it using existing variable item of, you know, you can't set item to item.tomorrow because item.tomorrow doesn't exist yet. So we need to set item.tomorrow to this value. And that would work a bit better. So we'll save and we'll do rerun. And you can see that the tomorrow key gets added right here at the end. And that's an impact of us doing this, uh, forEach loop gives us access, we can update the item. Now what's really cool about that though, is that the item is actually by reference. And what that means is that when we edit item, we're actually editing the original JSON test. So if I drag this underneath this loop, right, and once they just show me what JSON test 1 looks like at the end of all this, right, here's a variable JSON test 1, I will say, rerun again. And you can see now look, it's an array just like we had with a little square bracket up here. And here is the JSON, the first record JSON test 1, you see it sets tomorrow, the second one, also set to tomorrow, the third one also set to tomorrow. And right now, actually, tomorrow is always constant because based on now, so I could do based on item.createdAt, right, because that's one of the fields here, which is also a date field. And I can say, how about based on that, I want to make it tomorrow, that was tomorrow, that's contextual to the individual record. And if I did that, I just do a rerun. You see here I have 1, 1, 6, 8, 0, 0, and this 1, 6, 8, 0, 0, 9, 2, which is as opposed to 1, 6, 6, 8, 0, you know, 6, 5, right? And we can keep on scrolling through and we'll see it just sort of adds 24 hours to it, which is slick because what this means is we can augment data inside a foreach loop, which is really handy. You can take like data you might have gotten from an external data source or from a database. And you can, you know, add additional information to it. This is where you can do like calculated fields, right? Because we don't store calculated fields inside Xano. We store just the base data, and then we can calculate it in this kind of loop for whatever we're going to present to the front end. And that's, that's where you would handle the what would be the equivalent of calculated fields in something like, I don't know, Airtable. The, and so that's, that's all pretty good stuff. And then we can also inside here do like more of a reducer pattern in which we're going to collect information outside of it. Like if we wanted to just find out what was the, you know, what we, a silly thing that we could do here, we could find out the sum of the IDs, right? So I could create a new, create a variable here, and I will call it, you know, idsTotal. Right? And the value is just going to be served with 0. Right? And then I could say in here, and I could just, you know, run data manipulation, and run math and add to the existing variable of IDs total, the item.id. So say that, rerun. And then of course, we're not actually making use of IDs total anywhere. So we would need to find out inside the debugger at the end. But how about we have it actually return as data at the end. So instead of returning JSON test one, we just return the IDs total. And we do a rerun. And, you know, when we run through a lot of data, this can happen. So what we— the way we handle that is by just unchecking this box here. Excellent. And then the stop and debug that got triggered, which I didn't want, I want to go down and see the IDs total. Right. And you can see the sum of all the IDs is 79,800. Now that's not a particularly useful number, but it sort of shows how you can both use this kind of foreach loop to add data inside the loop and also use it to extract data from the loop by having a variable on the outside and then modifying that as you're sitting inside the loop. So this is the most common thing you're going to do, uh, because it's super convenient for running through data from the database, coming through data from external data sources.. But there are a couple of other situations which you might want to be using these other tools that are available to us. So let's talk about what those are. The first one is called a for loop. And a for loop is a more general form of just how we're going to iterate over a certain number of items. And so I can just do— so the way for us to look at that would be to do data manipulation, and I go to loops. And I'm gonna say, let's just do a for loop. And a for loop doesn't connect me like with a data source that I'm going to iterate over, it just wants a number. Right? So if I just want to go, you know, 10 times, right? And we just call index, that's perfectly reasonable. We might say that what I want to do here is just do— we could do a stop and debug, but we actually want the loop to run. So another useful tool for watching things flow is to use the utility function called debug_log, because debug_log is like stop and debug, except doesn't stop. And here I just want to report on what index is. So what we're going to do is we're going to index out what the for loop actually has to say. And this is actually pretty useful because computers don't think the way that we do. Computers think in terms of things based on zero. So you can see debug logs becomes available to me because we did debug log in here. And you can see this gave me an index of 0, 1, 2, 3, 4. And the reason why this is important is that the, the, the starts with zero every time it'll go from zero to nine. So if I tell it to go 10 times, will never show a 10. It will only show 0 through 9. And you can use this to go over like existing data records. Like you could use this to sort of reverse engineer the foreach loop if you want to. So I can just do this instead of looping over 10 loops, I can say loop over the count of JSON test. And, and then I could even if I wanted to, you know, just debug out, log out You know, I can say that, well, actually, let's not use debug log yet. Let's instead say let's do a plus here. And we're going to do a create variable. And I'm going to call it item, right, with a value of JSON test one. Get, get is great for being dynamic here. The index. Right. So get the index, update. And we can run that. And let's just do a very quick stop and debug. So I can show you what actually that item looks like, because you're gonna find actually just looks like the item that we would have had in the previous loop. We run, see, it's just that first item that we got from database. So we can use a for loop to basically re-engineer a foreach loop to do the exact same job. Now it has a little more ceremony to it. So why would you ever want to do this?. And one of the main reasons you want to do this is when the index matters, and the index is going to matter a lot when you're trying to run through two separate arrays, right, that are all the same. Like, let's just say for the sake of argument that I had an input here, let's just make a new input. We'll make it JSON type, just so it's nice and simple to deal with. And we'll say, you know, called data. And the— yeah, fine. And then we're going to say, well, in data, we're going to have an array. Of, you know, A, B, C. Okay, let's just say it's 3 records. We have to put them in double quotes because it's JSON in there. Okay. And then the— and then what we can do in here is we can say, for each of these items in the index, let's ask if there is a corresponding record in data. That would go with this, so that we want something going to the first record, second record, or third record. So we will now say we got the item. And now we're going to say, let's, let's just pull it in, we're going to do a data manipulation, we're going to create a variable, we're going to call it value., and we're going to do it from data, at path, get the index, right? So you can see how this is convenient, because now we're able to use the same index in two different arrays. And then we're going to update the item variable. And we're going to do database requests, no, sorry, data manipulation, update the variable. The variable, and we'll call it item dot, you know, data value. And I'll set the value of the value that we just extracted, right? So like, for the first one, it should be A, second one should be B, third one should be C, and then probably nulls after that. So let's just see if that modified the item the way we would have expected. So we should expect now the first item value would get populated with the A. Let's see if that works. Data value becomes A, which is exactly what we wanted. See, I scroll on down here, I don't see this data value that we just created here unless we take an explicit additional step, which we can do, which is to say we want to update our table to reinsert this value. And the way we do that is we would say the data manipulation, we're going to update JSON test 1, we're going to set it to JSON test 1, where we're setting at path of index, we're going to replace the old version of that object with a new version of the object, which is the thing we were calling item. So this sort of allows us to go the long way around to replicate what we would do in a foreach. This isn't useful on its own, but it illustrates what we can do in terms of running. The fact that the one time you'd want to do it is going to be when you're trying to go through those parallel arrays, right? You want to get things that are matching up just like we might want to do here, right? So I've got my A, B, and my C. Well, the first one has the A, second one has a B, the third one has a C, and the fourth one doesn't have anything at all, right? So it has a null because there was nothing matching. We were able to do that because we made use of the get filter based on that index by sort of walking those two arrays together. This is often called zipping. And the— because we're going, you know, it's kind of like a zipper, you know, with the two pieces of mail sort of coming together one by one by one as you're going on down the ladder here. The— and for more complicated use cases, this is actually a question I see quite frequently on the forum. Because it's like one step more complicated than the foreach because you need to access the index, which this doesn't give you, right? This just gives you the item of value that's in the array, whereas this just gives you the index. But from the value, you can't infer the index, but from the index, you can infer the value. So it's more work, but it's a very powerful— all right, let's, um, before we go too much further here, let's talk about, um, uh, sort of a side effect in here, which is that it might be that we didn't want anything to happen after we, you know, after, for example, if the loop didn't actually contain anything, right? So like, remember, we— this, this thing created all these nulls, right? Like, how can we have it not create those nulls? And a way for us to not create the nulls is to skip over, you know, updating JSON test in the event the value didn't contain anything. And so I can do a plus here. And we would say, let's just make a little if condition. So we'll say if the value is null— bool is not actually useful, null is great. If the value is null, then we're going to do something we haven't done yet, which is we're going to break. And we'll go to data manipulation, we'll go to loops, and we will break out of this loop. This is such a useful feature because it means— actually, in this case, we don't want to break, we just want to continue. Continue is also a super useful feature. We'll talk about break in just a second. The, um, so go back to data manipulation and we're going to go to loops and we're going to pick continue. All right, save. All right. Um, and we'll say save. Have been renamed. I don't know what it's talking about. I'm going to say skip. Um, and then we will just keep on circling around. So run that. So what happens here? So first I get my A and then I get my B, cuz what's happening here is in the event that sees that value turns out to be null, cuz it goes A, B, C, and there's no fourth item in that array. And because there's no fourth item, it hits this if the value is in fact null. You see, it doesn't even create that additional item. So instead of saying data.value equals null, Now it just doesn't create all and it allows us to skip all the stuff that follows. Another thing that might be handy here is you might decide that, you know, once you get to that point, you don't just wanna skip 'cause you don't worry about holes. You just know that you're done. And that would be, and for that we can use break. So let's go to data manipulation. We will go to loops. We will say loop break. And that would be, I don't know why it's bothering me about that. We'll just say to do over here. Comment that out, right? And then we can break out of the loop, which just means it won't even continue at that point. Because right now, if you take a look at the debugger, you'll see the foreach loop is happening and it's going through every single one of them and hitting a break, hitting a break, hitting a break, right? But it's going through a whole bunch of these. What if we just wanna do the first 3? Well, what you're gonna find is that this for loop is gonna become much, much shorter as a result of breaking out after there's no more data in that, you know, source array that we put into the input. Let's try running that. Okay, so we get the same basic results, which are the A, the B, you know, the, the C, et cetera. And then if I go over to my debugger, You see my for loop is actually much, much faster. See before it was like 1.3 seconds, now it's like a tenth of a second. And the reason for that is that there's nothing more for it to do. It knows I do 1, 2, 3, 1, 2, 3, 1, 2, 3, and then the fourth time through it says, wait, there's nothing here, I'm breaking out of the loop. So a break can be super handy to make things go faster. It can be super handy to make sure you're not gonna be hitting like invalid data, like especially like if you're zipping along and you know that one list is a lot longer than the other. Well, then you can sort of stop when there's no longer a match and sort of break out at that point. So that's continue and that's break. Those apply both to for-eaches as well as to for loops. So let's talk about the last thing that I think is worth discussing here, which is the even more generalized case of, of a looping flow control, which is a while loop. And a while loop is going to run as long as its underlying condition is true.. So basically, it's kind of like a for loop with an if statement, right? You're going to put at the very beginning, hey, while this situation is true, do your loop, do your loop, do your loop. So let's talk about how we'd use a for— a while loop to simulate a for loop. Because that sort of illustrates the mechanics, we'll do that kind of quickly here. So we'll, we create a while loop. And we'll set it up in just a second here. What I want to do is create a new variable. And I'm gonna call it counter and we'll set it to 0. Okay? And so we'll say while that counter is less than 10, save and save. We're going to then say, let's make sure that we do math and add to the counter, we're going to keep on adding 1, right, until finally it hits 10. And there's nothing more for us to do. So here, I'm just going to say, let's do a utility. And we're going to just do a little debug log here, like we've done before. And that debug log is going to contain the counter at that particular point. Let's make it so while the counter is less than 10, we're going to debug log out the counter and go plus 1, plus 1, plus 1. Okay, let's, let's try running that. So now we have the debug logs, right? And the— in fact, I'm just going to do a quick thing here. Counter is I'm not sure why my spacebar is not working right now. Filter, and we're gonna say, um, uh, replace, and we're gonna replace counter with the actual counter. Okay, so that way, that way it'll be a little more self-explanatory. So we'll save that and we'll run this. And you see the debug logs, right? So the data counter is 0, counter is 1, counter is 2. Now, the reason why this is significant is that once again, it's showing us counters 1 through counters 9, just like it did for the for loop, right? It's still zero-based. And so this allows— so what we've done is we've deconstructed— like, if a for loop is a deconstructed foreach loop, then a while loop is deconstructed for loop and allows us to do even more general things. And here's a cool thing that it can do, it can run forever. You can just say while true, you know, let's, let's do this in a completely silly way. So I could just say while true, is equal to true. And this loop now is at risk for running for infinity. Now, that's not really a good idea. So let's use break to manage it. And we'll say data manipulation, I'm going to say conditional if my counter, which I'm still adding 1 to every time, is equal to, I don't know, 10. Then we'll say we should break out of that loop. Right, so break becomes like a really important idea in while loops, particularly if we're not managing the condition all that carefully, right? So we have if, yep, and then we have the original. So the while is set up to run forever, because true is always true. But we keep on adding, adding, adding, and then when we add into count,, and then if counter hits 10, then we're gonna break out of that particular loop. Let's see if that works. And we'll keep on running the debug log, so we'll be able to see that. In fact, let's run a little more debug log here, which is a really useful way to approach it. We will say debug log abort, and remember to put before the breakout loop. If it goes afterwards, you know, it'll break and never run this line.. So we want to say abort, and then we'll break out. So, so what should happen here is the while loop just wants to go forever. But we're going to debug log out the value of the counter, we're gonna keep on adding 1 to it, ding, ding, ding, until finally hits 10. And when it hits 10, we're going to say we're going to abort and break out of loop. Debug log, ding, ding, ding, ding, wait one more, abort. See, so it's basically doing the exact same thing that the for loop would. Now, Why would you want to use this instead of using a for loop? A lot of the time, we're going to do that when things are like less consistent. And a good example of that is going to be when we're getting paginated data, like if you're getting data from, and this is pretty common, like from an external API request, which gives you like one page of data at a time. And you are getting page over page over page, you want, you don't know at the beginning, how many pages of data is going to give you. Right? So you can just run your API request, add information to like a collector variable or whatever that you might have defined outside, just like we did with the ID counter above, or, you know, append it to an array or whatever. And then you're going to keep on running those API requests until finally it says, I don't have anything left for you. And then you use the if condition here to say, then let's break out, or use the while, you know, condition you have at the top to say this is not something we want to continue with. You could use it for managing error situations, like you're going to say strike 1, strike 2, strike 3. I have another video on managing unreliable API connections from Xano in which I use a while loop for exactly this purpose. So the, the difference between a for loop and a while loop is in a for loop you know how many turns it's going to take, and in a while loop you don't know how many turns it's going to take. It is possible to simulate a while loop for loop by just saying the number of the for loop to like a really high number, like run a million times and then have, and then use a break, right, to bust out of it. And that sort of makes the for loop act like a while loop. You can make a while loop act like a for loop like we just did. But where they really shine is when you're making use of them in the way that's going to drive a distinct, you know, value, right, that is specific to each of these forms. For each is most useful because it gives you what, one, because it's very easy to use, but the thing that's really really key about it is that you can modify the data in place by using the sort of update variable call like we talked about at the beginning. A for loop is really useful because you get access to the index, and that allows you to reference things that are at the same position in the array, like go over multiple arrays that like are parallel data at the same index for the purpose of merging or for the purpose of comparison or whatever it might be. And a while loop is going to be at its maximum when you don't know how many iterations you're going to run. It's just going to be based on a condition, which could be simple or complex. And then the main risk that you're taking with it is the, you know, the fact that the condition might never be met and it runs infinitely, which is why I, as a matter of course, actually always implement something that looks like this, right? Always implement the counter thing, even if it's another thing I'm really testing. I use it as a reason check. Like, I can say if the, if the counter is over, I don't know, 100, right? Then there's no way this thing should have run 100 times. Let's stop it. That prevents me from being in an infinite while loop. So by turning— by making the while loop have some of the characteristics of a for loop by using this if and break, I get to protect myself from the risk of a while loop. And, and, but otherwise get all the flexibility of it. These are power tools. You're, you're gonna find the vast majority of the time a foreach loop is the right tool for the job. A minority of the time, a for loop is the right tool for the job. A smaller percentage of the time, particularly dealing with like paginated data, sort of the big use case I see, a while loop is going to be appropriate. But as you get into, you know, working with data, you're gonna find your own strange use cases. And I hope that you, you know, feel confident that you can go play with these different kinds of looping structures and combine them with control structures like the if-then to create the, you know, a marvelous process that's going to be really distinctive to your creation. And I hope if you've got questions about this or particular ideas about like great ways to be making use of this looping features, that you'll drop it in the comments. And if you have particularly hard problems that we get to talk about them over on StateChange. Either way, we'll see you next time.










