点赞 + 关注 + 收藏 = 学会了
整理了一个n8n小专栏,有兴趣的工友可以关注一下 👉 《n8n修炼手册》
在 n8n 中,If 组件 (在较新版本中通常直接称为 If 或属于 Filter 逻辑)是实现工作流逻辑分支 的核心。它的作用是根据设定的条件,将数据流导向不同的路径:满足条件的走 true 分支,不满足的走 false 分支。
If 组件的核心配置

使用 If 组件时,主要涉及以下4个关键部分的配置:
1、检查类型
首先需要确定你要比较的数据类型,n8n 提供了以下几种常见类型:
- String: 文本(如:标题是否包含某个词)。
- Number: 数值(如:价格是否大于 100)。
- Boolean: 布尔值(如:状态是否为 true)。
- Date: 日期(如:时间是否在今天之前)。
2、条件逻辑
在选定数据类型后,需要配置具体的判断规则:
- Value 1: 通常是从上一个节点拖入的动态数据(表达式)。
- Operator : 比较符号(例如:
Equal等于、Contains包含、Larger大于)。 - Value 2: 你要对比的目标值(可以是固定值,也可以是另一个动态变量)。
3、多条件组合
如果你有多个条件,可以利用 AND (所有条件必须同时满足)或 OR(满足其中之一即可)进行组合。

4、输出
当数据进入 If 节点时,它会逐条处理输入的对象:
- True 分支: 凡是符合条件的条目,会从节点上方的输出口流出,进入后续处理逻辑。
- False 分支: 不符合条件的条目,会从下方的输出口流出。

小试牛刀
我准备了一个很简单的工作流来讲解一下 If 组件,工作流的JSON我放在文末,有需要的工友可以导入到你的 n8n 测试。

这是一个猜拳游戏,用 Form 表单让用户提交他要出的招式。

然后用 Code 组件,使用 JS 的随机数,随机生成一个招式(石头、剪刀、布)。

通过后面的一系列 If 组件判断选择不同的分支走向。

最终用 Code 组件在浏览器控制台输出结果(用 JS 的 console.log(内容) 可以在控制台输出内容)。

工作流JSON:
swift
{
"name": "If - 猜拳游戏",
"nodes": [
{
"parameters": {
"formTitle": "剪刀石头布游戏",
"formFields": {
"values": [
{
"fieldLabel": "请选择你要出的",
"fieldType": "radio",
"fieldOptions": {
"values": [
{
"option": "剪刀"
},
{
"option": "石头"
},
{
"option": "布"
}
]
}
}
]
},
"options": {
"appendAttribution": false
}
},
"type": "n8n-nodes-base.formTrigger",
"typeVersion": 2.5,
"position": [
-496,
64
],
"id": "5c72b126-2df9-47e8-84e6-2c6e171c4a85",
"name": "On form submission",
"webhookId": "322004ca-25b6-4721-a7b4-05fdec68434a"
},
{
"parameters": {
"jsCode": "// 定义出拳选项\n// 剪刀: 0; 石头: 1; 布: 2\nconst choices = ["剪刀", "石头", "布"];\n// 生成随机索引(0、1、2 随机一个)\nconst randomIndex = Math.floor(Math.random() * choices.length);\n// 获取随机出拳结果\nconst cResult = choices[randomIndex];\n\nreturn {cResult};"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-272,
64
],
"id": "2b4967e0-d04d-4f0f-8433-c4b47840688b",
"name": "Code in JavaScript"
},
{
"parameters": {
"jsCode": "console.log(`平局,${$('On form submission').first().json['请选择你要出的']}`)\n\nreturn {}"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
176,
-32
],
"id": "f801bac9-58a1-4c6e-8c2a-ff572a8860f4",
"name": "平局"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 3
},
"conditions": [
{
"id": "ffa31204-a56d-42ee-9a42-3b91c8bfa434",
"leftValue": "={{ $('On form submission').item.json['请选择你要出的'] }}",
"rightValue": "={{ $json.cResult }}",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.3,
"position": [
-48,
64
],
"id": "33f1ba94-a2ae-4f51-b17f-9496a741c379",
"name": "先判断是否平局"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 3
},
"conditions": [
{
"id": "87925288-6538-41ad-863a-10f45c2a9e98",
"leftValue": "={{ $('Code in JavaScript').item.json.cResult }}",
"rightValue": "布",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.3,
"position": [
624,
-80
],
"id": "c15da568-72a4-4b6c-8562-cd7a499ba041",
"name": "判断电脑是否出了"布""
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 3
},
"conditions": [
{
"id": "41b2e14f-3d8f-4e9b-bc32-e737e0884633",
"leftValue": "={{ $('On form submission').item.json['请选择你要出的'] }}",
"rightValue": "剪刀",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "or"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.3,
"position": [
176,
160
],
"id": "6dafccd7-8341-4663-8336-c91e0356d904",
"name": "判断用户是否出了"剪刀""
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 3
},
"conditions": [
{
"id": "f9880e98-4964-4dd7-82e3-5e032015348b",
"leftValue": "={{ $('Code in JavaScript').item.json.cResult }}",
"rightValue": "石头",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.3,
"position": [
624,
400
],
"id": "2ba9925e-e2e9-4dce-b4cf-79c54421d9f6",
"name": "判断电脑是否出了"石头""
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 3
},
"conditions": [
{
"id": "bc4c6105-00bf-4503-8135-5f1563f42004",
"leftValue": "={{ $('On form submission').item.json['请选择你要出的'] }}",
"rightValue": "石头",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.3,
"position": [
400,
304
],
"id": "31c1184c-780d-4745-b56c-8f6fa3bc1b52",
"name": "判断用户是否出了"石头"1"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 3
},
"conditions": [
{
"id": "f494c323-33ec-44d1-9b4b-42c535b881e7",
"leftValue": "={{ $('Code in JavaScript').item.json.cResult }}",
"rightValue": "剪刀",
"operator": {
"type": "string",
"operation": "equals",
"name": "filter.operator.equals"
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.3,
"position": [
624,
160
],
"id": "e3cfbe9e-9b4e-4229-95b9-3c9af635a353",
"name": "判断电脑是否出了"剪刀""
},
{
"parameters": {
"jsCode": "console.log(`用户赢了。【用户:${$('On form submission').first().json['请选择你要出的']};电脑:${$('Code in JavaScript').first().json.cResult}】`)\n\nreturn {}"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
848,
64
],
"id": "2d3171c0-6837-4b88-a35b-1a20697ec11a",
"name": "用户赢"
},
{
"parameters": {
"jsCode": "console.log(`电脑赢了。【用户:${$('On form submission').first().json['请选择你要出的']};电脑:${$('Code in JavaScript').first().json.cResult}】`)\n\nreturn {}"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
848,
256
],
"id": "b5ff6fe5-fd73-45c6-ba9e-eb5883b0c86c",
"name": "电脑赢"
},
{
"parameters": {
"content": "## 用户选项\n- 剪刀\n- 石头\n- 布",
"height": 272
},
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-592,
-16
],
"id": "e57ba53f-955c-47d8-aca7-06e4de116cf7",
"name": "Sticky Note"
},
{
"parameters": {
"content": "## 电脑随机出一个剪刀石头布",
"height": 272,
"width": 208
},
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-320,
-16
],
"id": "20d7eebc-a70b-4cce-911a-9babc8149a47",
"name": "Sticky Note1"
}
],
"pinData": {},
"connections": {
"On form submission": {
"main": [
[
{
"node": "Code in JavaScript",
"type": "main",
"index": 0
}
]
]
},
"Code in JavaScript": {
"main": [
[
{
"node": "先判断是否平局",
"type": "main",
"index": 0
}
]
]
},
"先判断是否平局": {
"main": [
[
{
"node": "平局",
"type": "main",
"index": 0
}
],
[
{
"node": "判断用户是否出了"剪刀"",
"type": "main",
"index": 0
}
]
]
},
"判断电脑是否出了"布"": {
"main": [
[
{
"node": "用户赢",
"type": "main",
"index": 0
}
],
[
{
"node": "电脑赢",
"type": "main",
"index": 0
}
]
]
},
"判断用户是否出了"剪刀"": {
"main": [
[
{
"node": "判断电脑是否出了"布"",
"type": "main",
"index": 0
}
],
[
{
"node": "判断用户是否出了"石头"1",
"type": "main",
"index": 0
}
]
]
},
"判断电脑是否出了"石头"": {
"main": [
[
{
"node": "用户赢",
"type": "main",
"index": 0
}
],
[
{
"node": "电脑赢",
"type": "main",
"index": 0
}
]
]
},
"判断用户是否出了"石头"1": {
"main": [
[
{
"node": "判断电脑是否出了"剪刀"",
"type": "main",
"index": 0
}
],
[
{
"node": "判断电脑是否出了"石头"",
"type": "main",
"index": 0
}
]
]
},
"判断电脑是否出了"剪刀"": {
"main": [
[
{
"node": "用户赢",
"type": "main",
"index": 0
}
],
[
{
"node": "电脑赢",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {
"executionOrder": "v1",
"binaryMode": "separate",
"availableInMCP": false
},
"versionId": "5bd27111-528f-49e3-b874-9c0684a2eaee",
"meta": {
"instanceId": "8b30a8ba058f5126b4f4e9373018ce0596139a4d2028982510061e844b858b71"
},
"id": "6I0AXC4cNc6BxtxfRvScV",
"tags": []
}
在实际应用中,比如自动化库存预警,If 组件出现频率是非常高的。
假设你正在运行一个监控系统,通过 API 获取商品的库存数据,如果库存低于某个数值,就需要发送告警邮件;如果库存充足,则仅更新数据库记录。
使用技巧
- 处理空值 : 在判断字符串时,如果不确定数据是否存在,建议先配合
Is Empty或Exists操作符防止工作流报错。 - 复杂逻辑 : 如果你的逻辑分支超过两个(例如:低库存、中等库存、高库存),建议使用 Switch 节点,它可以根据不同的匹配值引出多个输出口,比嵌套多个 If 节点更简洁。
Switch 节点留下一讲在聊~
以上就是本文的全部内容啦,想了解更多n8n玩法欢迎关注《n8n修炼手册》👏
如果你有 NAS,我非常建议你在 NAS 上部署一套 n8n,搞搞副业也好,帮你完成工作任务也好 《『NAS』不止娱乐,NAS也是生产力,在绿联部署AI工作流工具-n8n》
点赞 + 关注 + 收藏 = 学会了