Using looping in your functions
The company in our data has several offices, and we need to report on each of them, not just the L.A. one. Using ATL, we can loop over every item in a list (array) by calling the forAll
function. The forAll
function takes an array to loop over, and an action to perform.
Going back to what we know about JSON (json.org), an object has curly brackets and an array has square brackets. The array we want to loop over is offices
in our data.
We’ll start by adding the forAll
function to the ATL, then add an action for it to perform. The easiest way to add an action is to include a user-defined function. We can use the user-defined function that we already have: our DescribeOffice subscript.
In the Main script, change the call to the DescribeOffice subscript to this:
[[forAll(WholeJSON.offices, DescribeOffice)]]
What is this ATL saying? It is telling Studio that for each office in the data, call the DescribeOffice subscript with that office.
Click Preview.
You should now see a list describing all six offices.
Okay, so let’s review. In this step of the tutorial, we used the forAll
function to loop over all the offices in our data, and we added a user-defined function as the action for the forAll
function to perform.
Next, we’ll mix things up a bit with the chooseAtRandom
function.