").itemMatching(currentNodeinputIndex)`"> Retrieve linked items from earlier in the workflow | n8n Docs
跳至内容

从工作流早期检索关联项#

节点输入数据中的每一项都链接回先前节点中用于生成它的项。如果您需要从比直接前一个节点更早的地方检索链接项,这将非常有用。

要访问工作流中之前链接的项目,请使用 ("").itemMatching(currentNodeinputIndex)

例如,考虑一个执行以下操作的工作流:

  1. 客户数据存储节点生成示例数据:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    [
    	{
    		"id": "23423532",
    		"name": "Jay Gatsby",
    		"email": "gatsby@west-egg.com",
    		"notes": "一直在询问关于绿灯的事??",
    		"country": "US",
    		"created": "1925-04-10"
    	},
    	{
    		"id": "23423533",
    		"name": "José Arcadio Buendía",
    		"email": "jab@macondo.co",
    		"notes": "很多人以他的名字命名。非常混乱",
    		"country": "CO",
    		"created": "1967-05-05"
    	},
    	...
    ]
    
  2. 编辑字段节点简化了这些数据:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    [
    	{
    		"name": "Jay Gatsby"
    	},
    	{
    		"name": "José Arcadio Buendía"
    	},
        ...
    ]
    
  3. 代码节点将电子邮件地址恢复给正确的人:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    [
    	{
    		"name": "Jay Gatsby",
    		"restoreEmail": "gatsby@west-egg.com"
    	},
    	{
    		"name": "José Arcadio Buendía",
    		"restoreEmail": "jab@macondo.co"
    	},
    	...
    ]
    

代码节点通过以下代码实现此功能:

1
2
3
4
for(let i=0; i<$input.all().length; i++) {
	$input.all()[i].json.restoreEmail = $('Customer Datastore (n8n training)').itemMatching(i).json.email;
}
return $input.all();
1
2
3
4
for i,item in enumerate(_input.all()):
	_input.all()[i].json.restoreEmail = _('Customer Datastore (n8n training)').itemMatching(i).json.email

return _input.all();

您可以从n8n网站 | itemMatchin使用示例查看并下载示例工作流。

优云智算