ForEach
Share
Explore

# ForEachForEach

🎓 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.

## ⁠⁠⁠

Formula: ForEach()
Category: Lists (Arrays)
ForEach()
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:
List

ForEach(List, Formula)

A list of items that you want Coda to individually perform an action on.

Formula

ForEach(List, Formula)

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: 1234⁠

First choose what you want to do to each item in the list
0
2
to
each number

Notice how it changes EACH of our original numbers

Output:
3456

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:
6787
ForEach() checking if the words starts with “ala”:
truetruefalsefalse
ForEach() deleting every vowel from the word:
lsklbmrknssrzn

## 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:
1234
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

1
2
3
4
5
6
Lesson
Lesson is for:
Turn on a lightsaber
Dark side
Light side
Fight with a lightsaber
Dark side
Light side
Learn to Pilot an X-Wing
Light side
Learn to Pilot a Tie-Fighter
Dark side
Learn to be really cruel
Dark side
Learn to be really compassionate
Light side
There are no rows in this table

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

1
2
3
4
5
6
7
8
View
Name
Drawn Towards
All lessons
Luke Skywalker
Light side
Luke Skywalker
Luke Skywalker
Luke Skywalker
Luke Skywalker
R2-D2
Light side
Lessons
[  ]
Dark side
Lessons
[  ]
Leia Organa
Light side
Lessons
[  ]
Beru Whitesun lars
Dark side
Lessons
[  ]
Biggs Darklighter
Dark side
Lessons
[  ]
Obi-Wan Kenobi
Light side
Lessons
[  ]
Anakin Skywalker
Dark side
Lessons
[  ]
There are no rows in this table

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.

### Student-Force Lessons

1
2
3
4
Done
Student
Lesson
Luke Skywalker
Turn on a lightsaber
Luke Skywalker
Fight with a lightsaber
Luke Skywalker
Learn to Pilot an X-Wing
Luke Skywalker
Learn to be really compassionate
There are no rows in this table

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:

### First

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.

### Second

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.

### ⁠⁠⁠

Done
Build an automatic onboarding task tracking system
Done
Create a “Get to know the team” page for a large organization
Done

Share