跳转到内容

JavaScript 速查 · 表达式里最常用的 30 个函数

第 3 章 · 第 3 节

表达式里能用的就是 JavaScript 标准库。但 80% 的场景只会反复用到那 30 个函数。这一节是你以后写表达式的”备忘录”——常用语法一处全收。

🔤 string-ops.txt
// 大小写 "hello".toUpperCase() → "HELLO" "HELLO".toLowerCase() → "hello" // 修剪 / 替换 " hi ".trim() → "hi" "hello world".replace("world", "n8n") → "hello n8n" "a-b-c-d".replaceAll("-", "_") → "a_b_c_d" // 切片 / 包含 "hello world".slice(0, 5) → "hello" "hello".substring(1, 3) → "el" "hello world".split(" ") → ["hello", "world"] "hello".includes("ell") → true "hello".startsWith("he") → true "hello".endsWith("lo") → true // 长度 / 反转 "hello".length → 5 "hello".split("").reverse().join("") → "olleh" // 模板(推荐,比 + 更清晰) `Hello ${name}, you are ${age}` → "Hello Alice, you are 30"
🔢 array-ops.txt
// 长度 / 取值 [1,2,3].length → 3 [1,2,3][0] → 1 [1,2,3].at(-1) → 3 // 最后一个 // 过滤 / 映射 / 找 [1,2,3,4].filter(x => x > 2) → [3,4] [1,2,3].map(x => x * 2) → [2,4,6] [1,2,3,4].find(x => x > 2) → 3 [1,2,3,4].findIndex(x => x > 2) → 2 // 聚合 [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) → 按 a 升序 // 去重 [...new Set([1,1,2,3,3])] → [1,2,3] // 包含 [1,2,3].includes(2) → true [1,2,3].some(x => x > 2) → true [1,2,3].every(x => x > 0) → true // 拼接 [1,2,3].join(",") → "1,2,3" [1,2].concat([3,4]) → [1,2,3,4] [...[1,2], ...[3,4]] → [1,2,3,4] // 推荐
🔢 number-ops.txt
// 转换 parseInt("123abc") → 123 parseFloat("12.5kg") → 12.5 Number("123.45") → 123.45 String(123) → "123" // 取整 / 取数 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 (0~1) // 格式化(保留 N 位小数) (3.14159).toFixed(2) → "3.14" // 判断 Number.isInteger(5) → true isNaN("abc") → true

日期(Luxon 注入,比原生 Date 好用)

Section titled “日期(Luxon 注入,比原生 Date 好用)”
📅 date-ops.txt
// 当前时间 $now → Luxon DateTime $today → 今天 00:00 $now.toISO() → "2026-05-13T10:23:45.000+08:00" $now.toFormat("yyyy-MM-dd") → "2026-05-13" $now.toFormat("yyyy 年 M 月 d 日") → "2026 年 5 月 13 日" // 加减时间 $now.plus({ days: 7 }) → 7 天后 $now.minus({ hours: 3 }) → 3 小时前 $now.startOf("month") → 当月 1 号 00:00 $now.endOf("week") → 本周末 23:59:59 // 比较 $now > DateTime.fromISO("2026-01-01") → true // 解析字符串 DateTime.fromISO("2026-05-13") → Luxon DateTime DateTime.fromFormat("13/05/2026", "dd/MM/yyyy") // 时区 $now.setZone("Asia/Shanghai") → 转到上海时区 $now.zoneName → 当前时区名
🗂 object-ops.txt
// 取值 / 设值 obj.field 或 obj["field"] obj.a?.b?.c (安全链) // keys / values / entries Object.keys(obj) → ["a", "b"] Object.values(obj) → [1, 2] Object.entries(obj) → [["a", 1], ["b", 2]] // 合并 / 复制 { ...obj1, ...obj2 } → 浅合并 JSON.parse(JSON.stringify(obj)) → 深拷贝 // 默认值 obj.field || "default" → field 为空时用 default obj.field ?? "default" → field 为 null/undefined 时(不包括 0/"")
ternary.txt
condition ? a : b → if-else 一行版 condition && a → 仅 condition 为真时 a condition || a → condition 为假时 a // 实战 ={{ $json.score > 60 ? '通过' : '挂科' }} ={{ $json.active && '✓' }} ={{ $json.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/https 开头
📦 json-ops.txt
JSON.stringify(obj) → 对象转字符串 JSON.stringify(obj, null, 2) → 格式化(缩进 2 空格) JSON.parse('{"a":1}') → 字符串转对象
  • 字符串:toUpperCase/trim/replace/slice/split/includes + 模板字符串
  • 数组:map/filter/find/reduce/some/every + 去重 [...new Set()]
  • 数字:parseInt/Math.round/.toFixed(2)
  • 日期:用 Luxon$now.plus({days}).toFormat()
  • 对象:Object.keys/values/entries + 安全链 ?.
  • 三元 ? : 比 IF 节点更适合简单条件
  • 正则在 {{ }} 里用双反斜杠

下一节类型转换与日期处理,把最常踩坑的几个细节吃透。