Switch#
使用 Switch 节点根据比较操作条件路由工作流。它类似于 IF 节点,但支持多个输出路由。
¥Use the Switch node to route a workflow conditionally based on comparison operations. It's similar to the IF node, but supports multiple output routes.
节点参数#
¥Node parameters
选择节点应使用的模式:
¥Select the Mode the node should use:
- 规则:选择此模式可为每个输出构建匹配规则。
¥Rules: Select this mode to build a matching rule for each output.
- 表达式:选择此模式可编写表达式以编程方式返回输出索引。
¥Expression: Select this mode to write an expression to return the output index programmatically.
节点配置取决于你选择的模式。
¥Node configuration depends on the Mode you select.
规则#
¥Rules
要使用此操作配置节点,请使用以下参数:
¥To configure the node with this operation, use these parameters:
- 创建路由规则以定义比较条件。
¥Create Routing Rules to define comparison conditions.
-
使用数据类型下拉菜单选择条件的数据类型和比较操作类型。例如,要创建特定日期之后的日期规则,请选择“日期和时间”>“之后”。
¥Use the data type dropdown to select the data type and comparison operation type for your condition. For example, to create a rules for dates after a particular date, select Date & Time > is after.
-
要输入到条件中的字段和值会根据你选择的数据类型和比较而变化。请参阅 可用数据类型比较 获取按数据类型划分的所有比较的完整列表。
¥The fields and values to enter into the condition change based on the data type and comparison you select. Refer to Available data type comparisons for a full list of all comparisons by data type.
-
重命名输出:启用此选项可重命名用于存放匹配数据的输出字段。输入你所需的输出名称。
¥Rename Output: Turn this control on to rename the output field to put matching data into. Enter your desired Output Name.
选择“添加路由规则”以添加更多规则。
¥Select Add Routing Rule to add more rules.
规则选项#
¥Rule options
你可以使用以下选项进一步配置此操作的节点:
¥You can further configure the node with this operation using these Options:
- 备用输出:选择当某个项目不符合任何规则或条件时,如何路由工作流。
¥Fallback Output: Choose how to route the workflow when an item doesn't match any of the rules or conditions.
-
无:忽略该项目。这是默认行为。
¥None: Ignore the item. This is the default behavior.
-
额外输出:将项目发送到额外的独立输出。
¥Extra Output: Send items to an extra, separate output.
-
输出 0:将项目发送到与符合第一条规则的项目相同的输出。
¥Output 0: Send items to the same output as those matching the first rule.
-
忽略大小写:设置评估条件时是否忽略字母大小写(已启用)或强制区分字母大小写(已禁用)。
¥Ignore Case: Set whether to ignore letter case when evaluating conditions (turned on) or enforce letter case (turned off).
- 放宽类型验证:设置是否让 n8n 根据你选择的运算符尝试转换值类型(开启)或不转换(关闭)。
¥Less Strict Type Validation: Set whether you want n8n to attempt to convert value types based on the operator you choose (turned on) or not (turned off).
- 将数据发送到所有匹配的输出:设置是否将数据发送到所有满足条件的输出(启用)或仅将数据发送到第一个满足条件的输出(禁用)。
¥Send data to all matching outputs: Set whether to send data to all outputs meeting conditions (turned on) or whether to send the data to the first output matching the conditions (turned off).
表达式#
¥Expression
要使用此操作配置节点,请使用以下参数:
¥To configure the node with this operation, use these parameters:
- 输出数量:设置节点应具有的输出数量。
¥Number of Outputs: Set how many outputs the node should have.
- 输出索引:创建一个表达式,用于计算哪个输入项应路由到哪个输出。表达式必须返回一个数字。
¥Output Index: Create an expression to calculate which input item should be routed to which output. The expression must return a number.
模板和示例#
¥Templates and examples
相关资源#
¥Related resources
有关在 n8n 中使用条件语句创建复杂逻辑的更多信息,请参阅 使用条件拆分。
¥Refer to Splitting with conditionals for more information on using conditionals to create complex logic in n8n.
Available data type comparisons#
String#
String data type supports these comparisons:
- exists
- does not exist
- is empty
- is not empty
- is equal to
- is not equal to
- contains
- does not contain
- starts with
- does not start with
- ends with
- does not end with
- matches regex
- does not match regex
Number#
Number data type supports these comparisons:
- exists
- does not exist
- is empty
- is not empty
- is equal to
- is not equal to
- is greater than
- is less than
- is greater than or equal to
- is less than or equal to
Date & Time#
Date & Time data type supports these comparisons:
- exists
- does not exist
- is empty
- is not empty
- is equal to
- is not equal to
- is after
- is before
- is after or equal to
- is before or equal to
Boolean#
Boolean data type supports these comparisons:
- exists
- does not exist
- is empty
- is not empty
- is true
- is false
- is equal to
- is not equal to
Array#
Array data type supports these comparisons:
- exists
- does not exist
- is empty
- is not empty
- contains
- does not contain
- length equal to
- length not equal to
- length greater than
- length less than
- length greater than or equal to
- length less than or equal to
Object#
Object data type supports these comparisons:
- exists
- does not exist
- is empty
- is not empty