Branch then return to main flow

I have an interator loop that is cycling through some data. I want to put a test in that loop, exectute some additional logic in some cases then return to the flow. I cannot for the life of me figure out how to do this basic thing in make.

A simplified example of what I am doing is bucketing data. Imagine some json data for a script that looks like this:

"dialogue": [
    { "speaker": "A", "line": "Hi. Nice to meet you. " },
    { "speaker": "A", "line": "My name is person A" },
    { "speaker": "B", "line": "Hi A, I'm B" },
   { "speaker": "A", "line": "Cool - we should hang" },

The end result I am trying to get to here is:

Hi. Nice to meet you. My name is person A

Hi A, I’m B

Cool - we should hang

The approach I was trying to do was:

Loop through line by line {
    Set a variable for the speaker
    Test vs  LastSpeaker. if speaker has changed {
         Add speaker label to the aggregate text
         Increment count of speakers
         Update LastSpeaker
    Add text to aggregate text
Do some stuff with the result

I think I am not “thinking in” yet. How would you do this?

One way to do this is first put the dialogue through an iterator:

Then, put a text aggregator right after it:

the iterator allows you to loop over the lines, while the text aggregator allows you to combine the values back in a different way.

You can use Get and Map functions for this. First, you map the array dialogue, and grab all the lines where speaker is A.

Map allows you to grab values out of complex stuff using filters. In this case it’s:
{{map(2.dialogue; "line"; "speaker"; "A")}}

Which can be read as: Give me all the lines inside dialogue where the speaker is “A”.

Then, with GET you can point to a specific line (the first, second, third etc.)

{ "speaker": "A", "line": "{{get(map(2.dialogue; "line"; "speaker"; "A"); 3.`__IMTINDEX__`)}}" },
    { "speaker": "B", "line": "{{get(map(2.dialogue; "line"; "speaker"; "B"); 3.`__IMTINDEX__`)}}" }

This creates a new array consisting of all lines said by A. With the get function, you grab one particular line at a specific place. do the exact same thing for B.

using the iterator and aggregator, you now loop through them all, putting the first occurence of A and then the first occurence of B. Then the second line A says, and after that the second line B says and so on.

Make sure to set the row separator as a comma and parse the json afterwards.

Thanks for the reply. I might try that, but it feels overly complex for such a simple thing. Reading it back I think there is likely additional steps to preserve the order of the dialogye too. I’m basically trying to migrate something into Make that I have working in javascript.

I was expecting to either be able to merge a branch back into the main flow (which I don’t now believe is possible) or maybe have it as a function of some sort (enterprise only?). Maybe I should just find a way to run some arbitary code, but that feels like cheating!