How to Determine if an Item Exists in another Array?

Describe the problem/error/question

I have two input nodes, one being a getAll node of Grist, the other one being a Switch node.

The getAll node returns a table that has a column called “ID2”.

The Switch node takes input of a previous Webhook node, thus its output contains is an one-item array where the “body” of the HTTP request is nested inside another JSON.

What I Want to Do:

From the Switch node, get {{ $json.body.data.id }}, find out if this value exists in {{ ID2 }} of the getAll node.

In the pictures shown above, the {{ $json.body.data.id }} is 14, and the {{ ID2 }} is an array consisting of number from 1 to 12. So the output if false.

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node


Expected output:

Information on your n8n setup

  • n8n version: 1.0.5
  • Database (default: SQLite): Postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main): (IDK where to find this)
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Debian GNU/Linux 11 (bullseye)

Hi @Bonislaw, welcome to the community!

Based on your example data it seems to me you can just remove the Switch1 node, as all its output branches are sent to the same subsequent node anyway. Or in other words, your Switch1 node might send data to output 0, 1, 2, or 3 but for the workflow it doesn’t make a difference.

With your subsequent Merge node you are, however, on the right track as the Merge node lets you compare two separate input branches using the Combine → Keep Matches and Combine Keep Non-Matches operation. Here’s a quick example showing the idea using simplified example data:

When running this example with an id value in the webhook that’s not part of your Grist list, the item would reach the bottom branch:

When the item exists, it would end up on the upper branch:

Hope this helps!

1 Like

Thank you very much for the reply! Your workflow works like magic.

There is still one problem, though. The switch node doesn’t work quite like the set node, which outputs one array at most. Current merge branch seems to only take the first branch of the switch node (in my case ‘0’). So if the input value falls into another branch, the merge node won’t pick up the data. You can replicate this problem by adding a mock switch node and set the output to a non-zero value. I’m not sure whether this is by design or a bug.

The switch node doesn’t work quite like the set node, which outputs one array at most. Current merge branch seems to only take the first branch of the switch node (in my case ‘0’).

From looking at your workflow it seems the Switch node serves no purpose since all items it returns are passed on to the same node anyway, regardless of their output.

Perhaps you want to simply add your Switch node after the Merge nodes to avoid this? The example Merge nodes I have provided will return the webhook item anyway, so you can still apply your Switch logic at a later stage.

1 Like

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