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?

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)

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      name: 'Frank',\n      age: 20\n    }\n  },\n  {\n    json: {\n      name: 'Jim',\n      age: 35\n    }\n  },\n  {\n    json: {\n      name: 'Stephan',\n      age: 24\n    }\n  }\n];"
      },
      "name": "Dummy Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        400,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$node[\"Dummy Data\"].data[\"age\"]}}",
              "operation": "larger",
              "value2": 21
            }
          ]
        }
      },
      "name": "Filter IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        600,
        0
      ]
    },
    {
      "parameters": {
        "functionCode": "return items.filter((item) => {\n  return item.json.age > 21;\n});"
      },
      "name": "Filter Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  items[0]\n];"
      },
      "name": "Only First Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        400
      ]
    }
  ],
  "connections": {
    "Dummy Data": {
      "main": [
        [
          {
            "node": "Filter IF",
            "type": "main",
            "index": 0
          },
          {
            "node": "Filter Function",
            "type": "main",
            "index": 0
          },
          {
            "node": "Only First Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

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:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "// $node returns always the data of the first item\nitems[0].json.nodeVariable = $node[\"Init Data\"].data[\"myNumber\"];\nitems[1].json.nodeVariable = $node[\"Init Data\"].data[\"myNumber\"];\nitems[2].json.nodeVariable = $node[\"Init Data\"].data[\"myNumber\"];\n\n// $item allows to access the data of the item with the given index\nitems[0].json.itemVariable = $item(0).$node[\"Init Data\"].data[\"myNumber\"];\nitems[1].json.itemVariable = $item(1).$node[\"Init Data\"].data[\"myNumber\"];\nitems[2].json.itemVariable = $item(2).$node[\"Init Data\"].data[\"myNumber\"];\n\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "myString",
              "value": "=text_{{$node[\"Init Data\"].data[\"myNumber\"]}}"
            }
          ]
        }
      },
      "name": "Set String",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      myNumber: 0\n    }\n  },\n  {\n    json: {\n      myNumber: 1\n    }\n  },\n  {\n    json: {\n      myNumber: 2\n    }\n  }\n];"
      },
      "name": "Init Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Set String": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Init Data": {
      "main": [
        [
          {
            "node": "Set String",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

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!