表达式速查表
第 7 部分 · 表达式速查表
写 workflow 时最高频的”查表”操作就是表达式。这一页把 80% 场景一网打尽。
n8n 内建变量
Section titled “n8n 内建变量” 📋 builtins.txt
$json 当前 item 的 JSON
$json.fieldName 当前 item 的某字段
$binary 当前 item 的二进制(附件)
$('NodeName').first() 引用节点的第一个 item
$('NodeName').last() 最后一个 item
$('NodeName').all() 全部 items 数组
$('NodeName').item 智能配对(慎用,见 §3.5)
$('NodeName').itemMatching(idx) 按下标取
$now 当前 Luxon DateTime
$today 今天 00:00
$workflow workflow 元信息 ({ id, name, active })
$execution execution 元信息 ({ id, mode })
$env 环境变量(自托管)
$input 节点输入
$itemIndex 当前 item 在数组里的下标
$jmespath($json, 'a.b[*].c') JMESPath 查询
$randomString(8) 随机字符串
📋 strings.txt
{{ "hello".toUpperCase() }} → HELLO
{{ "hello".length }} → 5
{{ " hi ".trim() }} → "hi"
{{ "a-b-c".replaceAll("-", "_") }} → "a_b_c"
{{ "hello world".split(" ") }} → ["hello", "world"]
{{ "hello".slice(1, 4) }} → "ell"
{{ "hello".includes("ell") }} → true
{{ "hello".startsWith("he") }} → true
模板:
={{ "Hi " + $json.name + ", you are " + $json.age }}
={{ `Hi ${$json.name}` }} ← 反引号包裹的模板字符串
📋 arrays.txt
{{ [1,2,3].length }} → 3
{{ [1,2,3].map(x => x * 2) }} → [2,4,6]
{{ [1,2,3,4].filter(x => x > 2) }} → [3,4]
{{ [1,2,3,4].find(x => x > 2) }} → 3
{{ [1,2,3,4].reduce((a,b) => a+b, 0) }} → 10
{{ [3,1,2].sort() }} → [1,2,3]
{{ [{a:1},{a:2}].sort((x,y) => x.a - y.a) }}
{{ [1,2,3].some(x => x > 2) }} → true
{{ [1,2,3].every(x => x > 0) }} → true
{{ [1,2,3].includes(2) }} → true
{{ [...new Set([1,1,2,3,3])] }} → [1,2,3] 去重
{{ [1,2,3].join(",") }} → "1,2,3"
{{ [1,2,3].at(-1) }} → 3 最后一项
📋 numbers.txt
{{ Number("123") }} → 123
{{ +"123" }} → 123 简写
{{ parseInt("123abc") }} → 123
{{ parseFloat("12.5kg") }} → 12.5
{{ Math.round(1.6) }} → 2
{{ Math.floor(1.9) }} → 1
{{ Math.ceil(1.1) }} → 2
{{ Math.abs(-5) }} → 5
{{ Math.max(1, 2, 3) }} → 3
{{ Math.min(1, 2, 3) }} → 1
{{ Math.random() }} → 0.xxx
{{ (3.14159).toFixed(2) }} → "3.14"
日期 (Luxon)
Section titled “日期 (Luxon)” 📋 dates.txt
当前时间:
{{ $now }} Luxon DateTime
{{ $now.toISO() }} "2026-05-15T10:23:45+08:00"
{{ $now.toISODate() }} "2026-05-15"
{{ $now.toMillis() }} 1715000000000
{{ $now.toFormat("yyyy-MM-dd") }} "2026-05-15"
{{ $now.toFormat("yyyy 年 M 月 d 日") }} "2026 年 5 月 15 日"
加减:
{{ $now.plus({ days: 7 }) }} 7 天后
{{ $now.plus({ hours: 3, minutes: 30 }) }}
{{ $now.minus({ months: 1 }) }} 1 个月前
{{ $now.startOf("day") }} 今天 00:00
{{ $now.startOf("month") }} 本月 1 号
{{ $now.endOf("week") }} 本周末
解析字符串:
{{ DateTime.fromISO("2026-05-15") }}
{{ DateTime.fromFormat("15/05/2026", "dd/MM/yyyy") }}
{{ DateTime.fromMillis(1715000000000) }}
时区:
{{ $now.setZone("Asia/Shanghai") }}
{{ $now.zoneName }} "Asia/Shanghai"
📋 objects.txt
{{ obj.field }} 访问
{{ obj["field"] }} 中文字段名等用 []
{{ obj?.a?.b?.c }} 安全链
{{ Object.keys($json) }} ["a", "b"]
{{ Object.values($json) }} [1, 2]
{{ Object.entries($json) }} [["a", 1], ["b", 2]]
{{ { ...obj1, ...obj2 } }} 浅合并
{{ JSON.parse(JSON.stringify(obj)) }} 深拷贝
默认值:
{{ obj.field || "默认" }} field 假值时
{{ obj.field ?? "默认" }} field 仅 null/undefined 时
条件 / 三元
Section titled “条件 / 三元” 📋 conditional.txt
{{ condition ? a : b }} if-else 一行
{{ cond1 ? a : cond2 ? b : c }} 链式
{{ $json.score > 60 ? '通过' : '挂科' }}
短路:
{{ active && "✓" }} active 真则 "✓"
{{ name || "(匿名)" }} name 空则 "(匿名)"
📋 regex.txt
{{ "hello123".match(/\d+/)[0] }} → "123"
{{ "a.b.c".replace(/\./g, "_") }} → "a_b_c"
{{ /\d+/.test("hello123") }} → true
常用:
/^\d+$/ 纯数字
/^[\w.-]+@[\w.-]+\.\w+$/ 邮箱
/^https?:\/\// 以 http(s) 开头
⚠ 表达式内的反斜杠要写两个
📋 json.txt
{{ JSON.stringify($json) }} 对象→字符串
{{ JSON.stringify($json, null, 2) }} 格式化
{{ JSON.parse('{"a":1}') }} 字符串→对象
下一节中英术语对照表。