IF Node Values Exist

Hello.

I can’t figure how to check if a value from an array of some node

[
{
"Source": "bbb"
}
] 

is exist in another array from another node

[
{
"Destination": "aaa"
},
{
"Destination": "bbb"
},
{
"Destination": "ccc"
}
] 

so that IF Node returns true and procceds.

Any ideas?

This would require some custom JS. I assume the array from another node is an array of multiple n8n items? If so, here’s a quick example I’ve whacked together using the $items method and JavaScript’s .includes(). You should be able to adjust it to your needs even if your data structure is slightly different:

Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "let my_array = [{\n  \"Destination\": \"aaa\"\n}, {\n  \"Destination\": \"bbb\"\n}, {\n  \"Destination\": \"ccc\"\n}]\n\nreturn my_array.map(e => { return { json: e } });"
      },
      "name": "Set Example Array",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.isInArray = $items(\"Set Example Array\").map(e => e.json.Destination).includes(item.Source);\nreturn item;"
      },
      "name": "Check if Item in Array",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    \"Source\": \"bbb\"\n  }\n}, {\n  json: {\n    \"Source\": \"ddd\"\n  }\n}] "
      },
      "name": "Set Items to Check",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json[\"isInArray\"]}}",
              "value2": true
            }
          ]
        }
      },
      "name": "In Array?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "Set Example Array": {
      "main": [
        [
          {
            "node": "Set Items to Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if Item in Array": {
      "main": [
        [
          {
            "node": "In Array?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Items to Check": {
      "main": [
        [
          {
            "node": "Check if Item in Array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

This will add an isInArray field to each input item, telling you whether your String is in the array:

You could the use this field in your IF node. Hope this helps getting started :slight_smile:

1 Like

Yes it’s working fantastic

I didn’t know the use case for Function Item node.

Is it used as i understand to loop through items returned from another node?

The documentation here

https://docs.n8n.io/getting-started/key-concepts.html#function

that says Check out this page to learn about the difference between the two. returns a 404 error page.

Where can i found this page to learn more?

Thank you again!

Glad to hear this is working, thanks a lot for confirming!

The correct link should be: Key Concepts | Docs, sorry for the 404. The main difference is indeed that the Function Item node loops through all incoming items (whereas the Function node gets executed only once).

In the above example workflow this would allow you to perform the lookup for multiple items as it would check for each input item whether the value appears in the referenced array.

Hope this makes sense :slight_smile:

1 Like

Hi @Mulen,

I was speaking to @maxT about this today and he suggested another approach that I did overlook earlier.

As a code-free approach, you could also use the Merge node’s Keep Key Matches mode to keep only the matching items:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "let my_array = [{\n  \"Destination\": \"aaa\"\n}, {\n  \"Destination\": \"bbb\"\n}, {\n  \"Destination\": \"ccc\"\n}]\n\nreturn my_array.map(e => { return { json: e } });"
      },
      "name": "Set Example Array",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    \"Source\": \"bbb\"\n  }\n}, {\n  json: {\n    \"Source\": \"ddd\"\n  }\n}] "
      },
      "name": "Set Items to Check",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        200
      ]
    },
    {
      "parameters": {
        "mode": "keepKeyMatches",
        "propertyName1": "Source",
        "propertyName2": "Destination"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Example Array",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set Items to Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Array": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Set Items to Check": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

image

The merge node also has a Remove Key Matches mode in case you want to find items that are not part of your array. Hope this makes life a tad easier for you :slight_smile:

2 Likes

Indeed i tried to replicate it in my setup and both ways now work.

Thank you very much again for your effort!

1 Like