Loop within loop?

Is it possible to run a loop inside a loop in order to concatenate different levels of the same data structure?

*Note: After concatenation, the objects must still behave as objects, meaning that when hovered on they display the Preview modal. (In one partial solution, I achieved concatenation, but lost object status because concatenation converted it to text.)

Like this:

I’ve tried everything I can think of, but coming up short here.

1 Like

Hey @Ander, not sure if this is the solution you’re looking for since it’s not exactly an array of arrays, but it gives you the ability to hover over the different levels of the arrays using the FormulaMap() function:

The formula in “desired” is:

=FormulaMap([TS Multiselect],concatenate(CurrentValue,Lookup([Table 5],S, CurrentValue.ToText()).[SB Multiselect] ))

1 Like

Thanks @Al_Chen_Coda !! This was very helpful.

I had to fiddle a bit longer to get a solution that will work in the project’s surrounding context (not visible in the screenshots), but I think I got it. Posted below for others who might be interested.


(1) Lookup() solution: FormulaMap([TS multiselect],Concatenate(CurrentValue,Lookup([Table 5],S,CurrentValue.ToText()).[SB multiselect] ))

(2) Filter() solution: FormulaMap([TS multiselect],Concatenate(CurrentValue,": ",filter([Table 5],S=CurrentValue.ToText()).[SB multiselect]))

(3) nested FormulaMap() solution: Sequence(1,count([TS multiselect]),1).FormulaMap(Concatenate([TS multiselect].Nth(CurrentValue),": ",FormulaMap([TS multiselect].[SB multiselect].Nth(CurrentValue),Concatenate(CurrentValue,", "))))

  • Note 1: I’d be interested to learn how to remove those trailing commas.
  • Note 2: I don’t understand the formula editor error prompt in this case: “Wrong argument type”
1 Like

The trailing comma happens because you are using the Concatenate function. You could also use the Join function which joins a list of items by a provided value (see formula page here).

The formula error is a usability issue with how we treat CurrentValue. Is the formula returning your desired result?

1 Like

@Al_Chen_Coda Yeah, I’d previously tried using join(), like this:

Sequence(1,count([TS multiselect]),1).FormulaMap(Concatenate([TS multiselect].Nth(CurrentValue),": ",join(", ",[TS multiselect].[SB multiselect].Nth(CurrentValue))))

It removes the trailing commas, but kills the object status of the list items being joined, I assume because join() converts them to text.

Anyway, the trailing commas are fine for the time being. And yes, the formula is returning the desired result, so it’s good to know that formula error is just a usability issue. Thanks for this follow-up.

1 Like

Trying to do something trivial to do with two for loops but surprisingly incredibly hard to do with coda.

Say I have two values, Ps (say p1…p5) and Bs (say b1…b10)
I have one table here I want a selection to allow users to choose among values, in csv form that would ultimately look like:
P1, B1, B5, B10
P2, B2, B3, B10

image

What I need is to then have a view that creates a row for each pair chosen. That is, in csv that would look like:
P1, B1
P1, B5
P1, B10
P2, B2

etc.

Essentially what I need is something like this pseudocode:
for each row r1 in Assignments:
for each b in r1.Bs:
AddRow(Pairs, Pairs.P, r1.p, Pairs.B, b)

I’ve tried many combinations of FormulaMap and Sequnece but the fundamental problem seems like any looping construct losses the scope of the parent loop. That is, if I tried to have a FormulaMap within a FormulaMap “CurrentValue” only has the inner scope. Another thing I tried is to first add all the Ps and then add all the Bs in a different formula but I run into the same issue. Don’t really see how coda can claim to be turing complete. Any ideas?

@mal

It’s unclear to me exactly what is your desired end result.

See if this helps you move you forward: