Switch node - multiple conditions


I am looking for a way to route some messages based on a property, lets say the topic. Using a switch node i can route the message. For example 'Foo' could be routed to output one and 'Bar' could be routed to output two.

What if if wanted to route both 'Foo' or 'Bar' to output one and only 'FooBar' to output two. Does anyone have any suggestions on the best way to do this.


Using a switch


or you could use a function...


switch(msg.topic) {
    case "foo":
    case "bar":
        return [msg,null]; //send msg to output 1
    case "foobar":
        return [null,msg]; //send msg to output 2

if you really want just 2 outputs and a switch node...


You could either route Foo and Bar to output one and two and FooBar to three and then join outputs 1 and 2 together. Alternatively use a regex test to check for Foo or Bar. Include start and end of string in the regex to make sure it doesn't find any string containing Foo and other stuff. So something like

[Edit] I guess that a regex will be more efficient than JSONata

Possibly yes (I dont advocate JSONata for something like this (it was a POC) - i'd recommend the change node with three outputs as first choice).

Thanks all, two great answers. I cant be thinking in a low-code frame of mind because i didn't think of simply using the wires to route the messages.

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