Airscript Learn to use Airtable scripts

3 ways to loop

Looping (or iterating) over the set of records returned from a table query will probably feature in most of your Airtable scripts. It is worth getting comfortable with how to do this. As ever in programming, there are a number of ways to approach looping and we’ll outline 3 JavaScript methods here.

It is also worth understanding each so that you can easily recognise what is happening when you view other developers’ scripts.

Before we get into the different methods let’s remember that the set of records returned by a table query is an array. We can iterate over each element of the array and take some action against each record. If you’re not familiar with the concept of an array take a look at the reference material here. Array indexes (the number that identifies an element within an array) are zero-based - the first element is identified by index 0, the second element by 1 and so on.

To keep things simple and to focus on the looping methods, we will query our table and output the primary field of the table to the output window. Our table, then, can be something simple like this:

The for loop

The “traditional” for loop is of the following form:

for (initialization, condition, final-expression) {
 // code to do something here
}

It takes three statements:

  • initialization: is executed once before the code runs and typically defines and initialises a counter variable that is used to keep track of the number of iterations
  • condition: this expression is evalued on each iteration and, if it returns true, the code block is executed
  • final-expression: this is executed at the end of each loop and typically increments the counter by 1

An example of a for loop in an Airtable script might look like this:


let table = base.getTable('People');
let query = await table.selectRecordsAsync();

for (let i = 0; i < query.records.length; i++) {
  output.text(query.records[i].name);
}

In this for loop we are setting the counter variable i to zero. In the condition statement we are saying “run the code block where i is less than the length of the query records array”. And the final-expression statement is incrementing the counter by 1.

So, let’s imagine that our query returns 10 records; the length of the query.records array is 10. Our counter i starts off with a value of 0; 0 is less than the array length of 10 so we know the code block will run; after it has run, we increment the counter by 1 to 1 and around we go again. Now, since i = 1 and 1 < 10, the code block will run a second time and we’ll increment the counter by 1 to 2. This will keep running for i <= 9. After i is incremented to 10, i is no longer less than 10 so the code block won’t run again and the loop ends.

In the code block that runs on each iteration of the loop, we’re outputing as text the ith element of the array. In our code we have:

query.records[i].name

  • query.records is our array
  • query.records[i] is the ithe element of the array
  • query.records[i].name is the name attribute of the ith element

Run the script and you’ll see the record names printed out like this:

The for-of loop

The for-of loop is a more concise way of expressing the code and takes the form:


for (variable of array) {
  // code to do something here
}

For our purposes, the code might look like this:


let table = base.getTable('People');
let query = await table.selectRecordsAsync();

for (let record of query.records) {
    output.text(record.name);
}

Again, query.records is our array and we define a variable record which takes the value of the record on each iteration. We output the name attribute of the record.

The forEach loop

The forEach method, when used with an arrow function, is even more concise and takes the form:

array.forEach(currentElement => // code to do something here );

Our Airtable script is:


let table = base.getTable('People');
let query = await table.selectRecordsAsync();

query.records.forEach(record => output.text(record.name));

Again, query.records is our array of records returned by the query. record is a variable which takes the value of the current record as we loop through the query.records array. This record is passed to the output.text method, which prints the record.name as before.

Loops within Airtable

All looping methods will work nicely within Airtable, but, in certain circumstances, the forEach method can’t be used. If, within a loop, you want to update, create or delete a record in Airtable, you would typically prefix this with the keyword await, so that the loop waits for the update/create/delete action to complete before moving onto the next iteration.

However, you can’t use await with a forEach, so, if you’re trying to script something along these lines, use a for or for-of loop. More discussion on this topic here.

Recap

  • for loop - the traditional method, specifying the counter
  • for-of a more concise method, works in all situations
  • forEach - the most concise, but not suitable if you want to create/delete/update within a loop