🎓 It’s important that we provide a bit of background on this formula before fully diving in. ForEach() has historically been named FormulaMap(), a piece of code that applies an action to every item on a list. However, the goal of Coda (and of no-code in general) is to make building accessible to all. Therefore, Coda elected to give FormulaMap() an alternate name that better describes its actions: ForEach(). Now, either formula can be used to accomplish the same function.
We at The Coda School strive for inclusivity—no code is for everyone! Because of this (and because, let’s be honest, “for each” is a much more straightforward), you will find that we teach and use the ForEach() version of this formula throughout our lessons.
Category: Lists (Arrays)
Run a formula for every item in a list
Or another way to say it...
Hey Coda, I’m going to give you a list of items - Can you do the exact same action with/to each item in the list?
ForEach() takes two parameters:
A list of items that you want Coda to individually perform an action on.
This is the formula Coda will perform on each item in the given list.
To summarize, ForEach() takes a list and then applies a set formula to each item on that list. Let’s take a look at how that looks in the examples below:
Take this list of numbers for example:
First choose what you want to do to each item in the list
Notice how it changes EACH of our original numbers
Now let’s see what it looks like when we apply the ForEach() formula to text values:
Here's some quick actions ForEach() can do to each word
ForEach() making them Upper Case:
ForEach() making them Lower Case:
ForEach() finding the words length: ForEach() checking if the words starts with “ala”: ForEach() deleting every vowel from the word:
Don’t Forget CurrentValue!
The phrase CurrentValue has a special meaning within the ForEach() formula. It is used to access the current value within the list during a loop.
Take this list for example: The first time ForEach() runs, the currentValue is 1 The second time ForEach() runs, the currentValue is 2 The third time ForEach() runs, the currentValue is 3 The fourth time ForEach() runs, the currentValue is 4
currentValue will be used almost every time you write a ForEach() formula
How To Use This
Let’s look at an example of how ForEach() can unlock some 🎩 magic 🐇 in your Coda docs.
You are a headmaster preparing to lead a Jedi school that teaches upcoming force users to master their powers, but you don’t discriminate between those drawn to the light-side or the dark-side.
Obviously though, depending on which side (dark or light) a student is drawn too, they will need different lessons with different foci.
Therefore, we are going to make three tables:
A template database called that holds the list of lessons available A students database called that holds one row per student A lessons database called holds one row per lesson per student
Lets start with a database of lessons where each lesson is tagged either as one meant for the dark-side, the light side, or both
Force School Lessons
Now that we have a set database of lessons, let’s populate these lessons for individual students depending on what side of the force they are drawn to with the click of a button.
Force School Students
This table below is the database of lessons that each individual student needs to complete. Click the button above in each students row to see their tasks populated and mark them off as complete/incomplete.
You can click into the buttons options to see the formula used to assign lessons, but written in plain english, this is what it is happening:
Filter the database to find only the rows appropriate to the current student based on their preference for the light side or dark side. This creates the list of all the classes that match their force preference. That is the first argument of your ForEach() formula. It is the list that we will perform an individual action on.
Now that we have our target list of lessons, we define the action that we will perform on each item.
The chosen action in this case is for each applicable lesson, add a new-row for that student to the table.
Thats it! There are so many possibilities with ForEach() and the best way to find those is to start building docs of your own using this magic formula! But first, let’s start with some activities.
Ready to master Coda yourself?