How to get single item or filter items?

Hi,

When I use a function node to return items[0] I get no results but when i use items.reverse() it works so I am ensuring it is an array. How can I apply filter and only pass selected items to next node ? And how can i choose only the first item to be sent to the next node?

1 Like

Welcome to the community!

There are multiple ways that can be done. Here some examples:

You can simply copy the JSON, click once into the n8n window and the paste (CTRL+v)

Thanks a lot Jan ! Now I understand how the JSON is transferred between the nodes.

Great! I have it on my to-do list since a long time to create documentation for the Function-Nodes. Until they are in place problems like that are sadly to expect.

Will be looking forward to that. I am also interested in how we can manipulate data from 2 separate source in one function node.

Do you mean two different inputs or from previous nodes and not just the direct input?

Yes, accessing previous node that has execution data and also if possible using 2 separate inputs(currently using merge node with append for this)

Accessing previous nodes is possible already. Do sadly not remember the exact syntax right now. Think was something like node("name") but have to check once I am in front of my computer again. Will then write you.

Multiple inputs are not possible yet. But for that you can simply use a Merge-Node in “pass through” in combination with the syntax I write you later.

Ok could check now. It is possible like that in the Function-Node:

// $node returns always the data of the first item
items[0].json.nodeVariable = $node["Init Data"].data["myNumber"];
items[1].json.nodeVariable = $node["Init Data"].data["myNumber"];
items[2].json.nodeVariable = $node["Init Data"].data["myNumber"];

// $item allows to access the data of the item with the given index
items[0].json.itemVariable = $item(0).$node["Init Data"].data["myNumber"];
items[1].json.itemVariable = $item(1).$node["Init Data"].data["myNumber"];
items[2].json.itemVariable = $item(2).$node["Init Data"].data["myNumber"];

return items;

Here in a workflow to make that a little bit clearer:

Did that solve the problem you had?

Hi Jan, sorry for extremely late reply. Yes it did thank you very much

No problem. Great to hear! Have a nice day!