Skip to content

循环处理项目(Loop Over Items)#

循环处理项节点可帮助你在需要时循环处理数据。

🌐 The Loop Over Items node helps you loop through data when needed.

该节点保存原始输入数据,并在每次迭代时,通过 loop 输出返回预定义数量的数据。

🌐 The node saves the original incoming data, and with each iteration, returns a predefined amount of data through the loop output.

当节点执行完成时,它会将所有处理过的数据合并,并通过 done 输出返回。

🌐 When the node execution completes, it combines all of the processed data and returns it through the done output.

何时使用“循环遍历项目”节点(When to use the Loop Over Items node)#

默认情况下,n8n 节点设计为处理一系列输入项(有些例外,详见下文)。根据你的目标,通常在工作流中不需要使用“循环处理项”节点。你可以在 n8n 循环处理 页面了解更多关于 n8n 如何处理多项内容的信息。

🌐 By default, n8n nodes are designed to process a list of input items (with some exceptions, detailed below). Depending on what you're trying to achieve, you often don't need the Loop Over Items node in your workflow. You can learn more about how n8n processes multiple items on the looping in n8n page.

以下链接重点介绍了“循环遍历项目”节点的一些应用场景:

🌐 These links highlight some of the cases where the Loop Over Items node can be useful:

  • 循环直到处理完所有项目:描述了“循环遍历项目”节点与普通项目处理的不同之处,以及何时可能希望使用此节点。
  • 节点例外:概述了在某些特定情况下和节点上,你可能需要使用“循环遍历项目”节点来手动构建循环逻辑。
  • 避免速率限制:演示如何批量发送 API 请求以避免来自其他服务的速率限制。

节点参数(Node parameters)#

批处理大小(Batch Size)#

请输入每次调用返回的项数。

🌐 Enter the number of items to return with each call.

节点选项(Node options)#

重置(Reset)#

如果开启,节点将在每次循环时使用当前输入数据重新初始化。 当你希望“循环处理项目”节点将传入的数据视为一组新的数据,而不是前一项目的延续时,请使用此选项。

🌐 If turned on, the node will reset with the current input-data newly initialized with each loop. Use this when you want the Loop Over Items node to treat incoming data as a new set of data instead of a continuation of previous items.

例如,当你事先不知道需要多少页时,你可以使用带有重置选项的“循环遍历项目”节点和 If 节点 来查询分页服务。循环一次查询一页,执行任何处理,并递增页码。循环重置确保循环将每次迭代视为一组新的数据。If 节点评估退出条件,以决定是否执行另一轮迭代。

🌐 For example, you can use the Loop Over Items node with the reset option and an If node to query a paginated service when you don't know how many pages you need in advance. The loop queries pages one at a time, performs any processing, and increments the page number. The loop reset ensures the loop recognizes each iteration as a new set of data. The If node evaluates an exit condition to decide whether to perform another iteration or not.

包含有效的终止条件

对于像上面描述的示例这样的工作流,包含一个有效的循环终止条件是至关重要的。如果你的终止条件永远不匹配,工作流的执行将会陷入无限循环。

启用后,你可以通过将参数表示方式从固定切换为表达式来调整重置条件。表达式计算的结果将决定节点何时重置项目处理。

🌐 When enabled, you can adjust the reset conditions by switching the parameter representation from Fixed to Expression. The results of your expression evaluation determine when the node will reset item processing.

模板和示例(Templates and examples)#

Template widget placeholder.

从两个不同的源读取 RSS 源(Read RSS feed from two different sources)#

此工作流允许你使用“循环处理项目”节点从两个不同的来源读取 RSS 提要。在工作流中需要使用“循环处理项目”节点,因为“RSS 提要读取”节点只处理接收到的第一个项目。你还可以在 n8n.io 上找到该 工作流

🌐 This workflow allows you to read an RSS feed from two different sources using the Loop Over Items node. You need the Loop Over Items node in the workflow as the RSS Feed Read node only processes the first item it receives. You can also find the workflow on n8n.io.

该示例演示了如何构建工作流,但假设你已经熟悉 n8n。若要构建你的第一个工作流,包括学习如何向工作流中添加节点,请参考 亲自试一试

🌐 The example walks through building the workflow, but assumes you are already familiar with n8n. To build your first workflow, including learning how to add nodes to a workflow, refer to Try it out.

最终工作流程如下所示:

🌐 The final workflow looks like this:

Workflow preview placeholder.

复制上面的工作流文件并粘贴到你的实例中,或者按照以下步骤手动构建:

🌐 Copy the workflow file above and paste into your instance, or manually build it by following these steps:

  1. 添加手动触发器。
  2. 添加代码节点。
  3. 将此代码复制到代码节点中:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    return [
    	{
    		json: {
    			url: 'https://medium.com/feed/n8n-io',
    		}
    	},
    	{
    		json: {
    			url: 'https://dev.to/feed/n8n',
    		}
    	}
    ];
    
  4. 添加循环遍历项目节点。

  5. 配置循环处理项目:在 批处理大小 字段中将批量大小设置为 1
  6. 添加 RSS 源读取节点。
  7. 选择 执行工作流。这会运行工作流,将数据加载到 RSS Feed 读取节点中。
  8. 配置 RSS Feed 阅读:将输入中的 url 映射到 URL 字段。你可以通过从 输入 面板拖放来完成此操作,或使用此表达式:{{ $json.url }}
  9. 选择 执行工作流 来运行工作流并查看生成的数据。

检查节点是否已处理所有项目(Check that the node has processed all items)#

要检查节点是否仍有要处理的项目,请使用以下表达式:{{$("Loop Over Items").context["noItemsLeft"]}}。该表达式返回一个布尔值。如果节点仍有数据需要处理,表达式返回 false,否则返回 true

🌐 To check if the node still has items to process, use the following expression: {{$("Loop Over Items").context["noItemsLeft"]}}. This expression returns a boolean value. If the node still has data to process, the expression returns false, otherwise it returns true.

获取节点的当前运行索引(Get the current running index of the node)#

要获取节点当前运行的索引,请使用以下表达式:{{$("Loop Over Items").context["currentRunIndex"];}}

🌐 To get the current running index of the node, use the following expression: {{$("Loop Over Items").context["currentRunIndex"];}}.