Iteratively append items to a list
Using the For Each and Append nodes to iteratively edit and append items to a list.
A common use case when building Kitchn automations is that you have a list of data and for each of its items you want to perform a certain task and then append the adjusted items to a new list.
In many cases, this is more easily achieved by using the Home: Patch JSON TiKiLa List node that can apply a set of patches to each item in a list, but sometimes, you want to manually loop over the items with a For Each node.
The pattern below shows a typical building pattern that you can incorporate:
In this setup, we have a For Each node that loops over the list of items, we have an Extract node that transforms the data (this could be any other node or group of nodes), and finally we have an Append node that appends the items into a new list.
Even though we want to start by looping over the items, we actually start by resetting the Append node. Only once it is reset, we will start the loop. While this might seem counterintuitive at first, it ensures we always have a clean, empty list that we are appending to before starting the loop.
In short:
- First, reset the Append node to make sure to have an empty list
- Once it is reset, start the loop
- After each append, go to the next item in the For Each loop
- Once we are done, we can do something with the new list of items
Last updated on September 26, 2023