Files
n8n-docs/docs/data/data-mapping/itemmatching.md
Kartik Balasubramanian 96e6ba167d Revamp the data section of n8n docs (#4077)
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
2026-02-25 14:30:33 +00:00

2.0 KiB

description, contentType
description contentType
How to use `("<node-name>").itemMatching(currentNodeinputIndex)` howto

Accessing linked items in the Code node

Every item in a node's input data links back to the items used in previous nodes to generate it. This is useful if you need to retrieve linked items from further back than the immediate previous node.

To access the linked items from earlier in the workflow, use ("<node-name>").itemMatching(currentNodeinputIndex).

For example, consider a workflow that does the following:

  1. The Customer Datastore node generates example data:
    [
    	{
    		"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"
    	},
    	...
    ]
    
  2. The Edit Fields node simplifies this data:
    [
    	{
    		"name": "Jay Gatsby"
    	},
    	{
    		"name": "José Arcadio Buendía"
    	},
        ...
    ]
    
  3. The Code node restores the email address to the correct person:
    [
    	{
    		"name": "Jay Gatsby",
    		"restoreEmail": "gatsby@west-egg.com"
    	},
    	{
    		"name": "José Arcadio Buendía",
    		"restoreEmail": "jab@macondo.co"
    	},
    	...
    ]
    

The Code node does this using the following code:

=== "JavaScript" js 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" ```python for i,item in enumerate(_input.all()): _input.all()[i].json.restoreEmail = _('Customer Datastore (n8n training)').itemMatching(i).json.email

return _input.all();
```

You can view and download the example workflow from n8n website | itemMatching usage example.