Reference variable in prior iteration of a loop

I have a loop where for the first item, it creates its content, and then for every iteration coming after, I’d like to use the content from the prior iteration as context for that current iteration so the overall output doesn’t repeat itself.

That said, I can’t seem to find the best way to get the value for the prior iteration as I’ve run into issues where in the first iteration, that variable (set node) cannot be resolved as it does not exist yet among many other issues.

TLDR - what is the best way to access the output of a prior generation of a loop, especially when that variable does not exist in the first generation of the loop?

Any help would be greatly appreciated.

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

@workflowsy , you haven’t provided your solution to look into. Anyway, the document you need to refer to is Output of other nodes | n8n Docs. Note that you would refer to the previous iteration with expression $runIndex-1. Here’s the working example.

Note that the initial data is

[
  {
    "item": 1
  },
  {
    "item": 2
  },
  {
    "item": 3
  }
]

and the output (“Done” node) becomes

[
  {
    "item": 1,
    "previous_item": null
  },
  {
    "item": 2,
    "previous_item": 1
  },
  {
    "item": 3,
    "previous_item": 2
  }
]

The magic expression is {{ $runIndex ? $("Loop Over Items").first(1, $runIndex-1).json.item : null }}. It checks the output of the Loop for the previous iteration with $("Loop Over Items").first(1, $runIndex-1).

Hopefully you can adjust the example to your own needs.

1 Like

@ihortom - Thank you so so much! That runIndex is hopefully the missing piece of the puzzle. Let me give that a shot and see if it all works.

Also here is a screenshot of my current configuration. If there is anything glaring that you recommend I change with this, let me know.

I’ll get back to you shortly once I’ve tested out your approach. I think this will hopefully address the unresolved variable issue I was talking about but we’ll see.

Hey @ihortom - So I tried your approach. I think what I’m going for is slightly different than what you proposed. I’m looking to either the output from the Create Chapter Content or get the prior chapter context for the prior iteration of my loop. Ex. If the first iteration generates chapter 1, I want chapter 2 to know the context of chapter 1 so it knows not to repeat itself.

Based on that, do you have any alternative recommendations?

@workflowsy , I do not see why you cannot use the solution I provided to this scenario too. The only difference is, instead of data in the Loop node you will be checking the data produced by the node that generates chapters.

To be more specific, if the node creating chapters is called “Create chapter”, you will reference its outcome of its previous iteration with something like {{ $("Create chapter").first(0, $runIndex-1) }}. The current iteration would be just {{ $("Create chapter").first() }}. Note that the function first() uses 0 as the first argument as we check the only output it has (which is zero-based, 0 by default) unlike Loop node where we checked the 2nd output.