If node used to compare one array contains any item from a second array

I am trying to compare one array from a previous node, search and see if a term is listed inside one of the items in an array while still using wildcards.

  • n8n version:1.47.2
  • Database SQLite
  • n8n EXECUTIONS_PROCESS setting: own
  • Running via docker
  • Operating system: Windows Server

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

hello @Justin_Herman

can you provide an example of the Received array?

I am trying to find one of a few strings in this array. As an example lets say I wanted to find “apple.com”, “outlook.com”, “Frontend”, “bacon-bits”. I want the If node to trigger positive because the term “outlook.com” is found at least once. The list of search terms I am looking for will grow so I want to be able to have one node that can look to see if any of the contents of ArrayB exist in ArrayA.

"Received": 
[
"from SJ.namprd17.prod.outlook.com (2603::392::9) by SJ.namprd7.prod.outlook.com with HTTPS; Tue, 30 Jul 2024 20:27:11 +0000",
"from DM.namprd02.prod.outlook.com (2603::1f4::23) by SJ.namprd7.prod.outlook.com (2603::a03:392::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.7807.29; Tue, 30 Jul 2024 20:27:08 +0000",
"from DS.namprd01.prod.outlook.com (2603:::cafe::4b) by DM.outlook.office365.com (2603::5:1f4::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.7784.34 via Frontend Transport; Tue, 30 Jul 2024 20:27:07 +0000",
]

you can do it with a more complex filter. For each whitelisted server it will join all Received entries and checks that the whitelisted server string is a substring of the joined Received entries. There is a limitation that you can’t use a comma delimiter for join method if the whitelist array contains anything with commas.

{{ $json.whitelist.filter(s => $json.Received.join(",").indexOf(s) > 0).length }}

Example:

1 Like

That worked, thank you.

If I wanted to compare an item from an Array to a string could I use the same expression?

Yes, but you will need to remove the join() method as it’s for Arrays.

Other option is to split the Array with Split Out node and use the default IF’s string method

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.