在 Code 节点中访问关联的数据项
节点输入中的每个数据项,都关联到前面节点用于生成它的数据项。需要从比紧邻前一节点更早的位置读取关联数据时,这很有用。
要访问工作流更早位置的关联数据项,使用 ("<node-name>").itemMatching(currentNodeinputIndex)。
例如,考虑以下工作流:
-
Customer Datastore 节点生成示例数据:
[
{
"id": "23423532",
"name": "Jay Gatsby",
"email": "gatsby@west-egg.com",
"notes": "Keeps asking about a green light??",
"country": "US",
"created": "1925-04-10"
},
{
"id": "23423533",
"name": "José Arcadio Buendía",
"email": "jab@macondo.co",
"notes": "Lots of people named after him. Very confusing",
"country": "CO",
"created": "1967-05-05"
},
...
] -
Edit Fields 节点简化数据:
[
{
"name": "Jay Gatsby"
},
{
"name": "José Arcadio Buendía"
},
...
] -
Code 节点为正确的人员恢复邮箱地址:
[
{
"name": "Jay Gatsby",
"restoreEmail": "gatsby@west-egg.com"
},
{
"name": "José Arcadio Buendía",
"restoreEmail": "jab@macondo.co"
},
...
]
Code 节点使用以下代码完成:
JavaScript
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();
Python
for i,item in enumerate(_input.all()):
_input.all()[i].json.restoreEmail = _('Customer Datastore (n8n training)').itemMatching(i).json.email
return _input.all();
可以在 n8n 网站:itemMatching 使用示例中查看和下载此工作流。
相关资源
本文为社区中文翻译。查看对应英文原文