通过交互式动手教程学习代码节点(JavaScript)基础知识
中级
这是一个Miscellaneous领域的自动化工作流,包含 12 个节点。主要使用 Set, Code, SplitOut, ManualTrigger 等节点。 通过交互式动手教程学习代码节点(JavaScript)基础知识
前置要求
- •无特殊前置要求,导入即可使用
使用的节点 (12)
分类
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
"meta": {
"instanceId": "e409ea34548a2afe2dffba31130cd1cf2e98ebe2afaeed2a63caf2a0582d1da0"
},
"nodes": [
{
"id": "e36b580a-9314-453c-88d6-7ffb948e79a8",
"name": "1. 示例数据",
"type": "n8n-nodes-base.set",
"position": [
500,
480
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "38ce3db6-ce1d-4091-9645-39e674ad1782",
"name": "users",
"type": "array",
"value": "=[{\"firstName\":\"Alice\",\"lastName\":\"Smith\",\"birthDate\":\"1990-05-15\"},{\"firstName\":\"Bob\",\"lastName\":\"Jones\",\"birthDate\":\"1985-11-22\"},{\"firstName\":\"Charlie\",\"lastName\":\"Brown\",\"birthDate\":\"2001-02-10\"}, {\"firstName\":\"Alex\",\"lastName\":\"Garcia\",\"birthDate\":\"1995-07-30\"}]"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "d77a1d9a-376a-451c-9fc3-174a60fb787a",
"name": "2. 拆分用户数据",
"type": "n8n-nodes-base.splitOut",
"position": [
720,
480
],
"parameters": {
"options": {},
"fieldToSplitOut": "users"
},
"typeVersion": 1
},
{
"id": "39ab2c6f-11ce-4e1e-a6c7-327d758f6d0c",
"name": "3. 处理每个用户",
"type": "n8n-nodes-base.code",
"position": [
1040,
480
],
"parameters": {
"mode": "runOnceForEachItem",
"jsCode": "// This code runs for EACH user individually.\n\n// 1. Get the data for the current item\nconst user = $input.item.json;\n\n// 2. Perform operations\nconst fullName = `${user.firstName} ${user.lastName}`;\n\nconst birthDate = new Date(user.birthDate);\nconst ageDiffMs = Date.now() - birthDate.getTime();\nconst ageDate = new Date(ageDiffMs);\nconst age = Math.abs(ageDate.getUTCFullYear() - 1970);\n\nconsole.log(`Processing user: ${fullName}, Age: ${age}`);\n\n// 3. Return the new data, keeping the original fields\nreturn {\n ...user, // This keeps the original data (firstName, lastName, etc.)\n fullName: fullName,\n age: age\n};"
},
"typeVersion": 2
},
{
"id": "9f4aa08e-f15f-4215-843c-87997e6d6cff",
"name": "5. 计算平均年龄",
"type": "n8n-nodes-base.code",
"position": [
1840,
480
],
"parameters": {
"jsCode": "// This code runs only ONCE for ALL users.\n\n// 1. Get all items from the previous node\nconst allUsers = $items();\n\n// 2. Perform an aggregation\nconst totalAge = allUsers.reduce((sum, item) => {\n return sum + item.json.age;\n}, 0);\n\nconst userCount = allUsers.length;\nconst averageAge = totalAge / userCount;\n\nconsole.log(`Calculated average age for ${userCount} users.`);\n\n// 3. Return a single new item with the result\nreturn [\n {\n json: {\n totalUsers: userCount,\n averageAge: parseFloat(averageAge.toFixed(2)) // Format to 2 decimal places\n }\n }\n];"
},
"typeVersion": 2
},
{
"id": "2e7f0710-0b4a-450e-8e87-591c5324f5c7",
"name": "4. 获取外部数据(高级)",
"type": "n8n-nodes-base.code",
"position": [
1440,
480
],
"parameters": {
"mode": "runOnceForEachItem",
"jsCode": "// ADVANCED: This code calls an external API for each user.\n\n// 1. Get the data for the current item\nconst user = $input.item.json;\n\n// 2. Use a helper function to make an HTTP request\n// We'll use a free API to guess the gender based on the first name.\nconst url = `https://api.genderize.io?name=${user.firstName}`;\n\nconsole.log(`Fetching external data for ${user.firstName} from ${url}`)\n\n// this.helpers.httpRequest is a powerful built-in function.\n// We use 'await' because it's an asynchronous operation.\nconst response = await this.helpers.httpRequest({ url: url, json: true });\n\n// 3. Return the original data merged with the new API data\nreturn {\n ...user, // Keep all existing data (fullName, age, etc.)\n gender: response.gender,\n genderProbability: response.probability\n};"
},
"typeVersion": 2
},
{
"id": "809e91c4-02b6-4d12-b8f0-e9cd96d92c24",
"name": "便签",
"type": "n8n-nodes-base.stickyNote",
"position": [
440,
240
],
"parameters": {
"color": 5,
"width": 440,
"height": 420,
"content": "#### ▶️ 起点:示例数据"
},
"typeVersion": 1
},
{
"id": "bc8ec047-8595-4a07-8422-09779f8490c0",
"name": "便签1",
"type": "n8n-nodes-base.stickyNote",
"position": [
900,
80
],
"parameters": {
"color": 7,
"width": 380,
"height": 580,
"content": "#### ⚙️ 第一课:处理每个项目"
},
"typeVersion": 1
},
{
"id": "acd0b48a-ca88-4dae-ab19-b91db5050e8b",
"name": "便签2",
"type": "n8n-nodes-base.stickyNote",
"position": [
1300,
60
],
"parameters": {
"color": 6,
"width": 380,
"height": 600,
"content": "#### 🚀 高级第二课:使用辅助函数"
},
"typeVersion": 1
},
{
"id": "da000098-d03f-40a9-b017-fab8223a6afc",
"name": "便签3",
"type": "n8n-nodes-base.stickyNote",
"position": [
1700,
80
],
"parameters": {
"color": 7,
"width": 380,
"height": 580,
"content": "#### ⚙️ 第三课:一次性处理所有项目"
},
"typeVersion": 1
},
{
"id": "5c84ce9f-32d0-4222-9b1a-3887677bdabe",
"name": "6. 创建二进制文件(专家级)",
"type": "n8n-nodes-base.code",
"position": [
2240,
480
],
"parameters": {
"jsCode": "// LESSON 4 (EXPERT): Creating a binary file.\n\n// This node runs only ONCE for ALL items.\n\n// 1. Get all items from the previous node\nconst allUsers = $(\"4. Fetch External Data (Advanced)\").all();\n\n// 2. Create a CSV string from the data\nlet csvContent = \"FullName,Age,GenderGuess,ProcessedBy\\n\"; // CSV Header\n\nfor (const item of allUsers) {\n const user = item.json;\n const row = `\"${user.fullName}\",${user.age},${user.gender},n8n`;\n csvContent += row + \"\\n\";\n}\n\n// 3. Use a helper to create a binary file from the string\nconst binaryData = await this.helpers.prepareBinaryData(Buffer.from(csvContent), 'user_report.csv');\n\n// 4. Return a new item containing the binary data\nreturn [\n {\n json: {\n reportGenerated: new Date().toISOString(),\n userCount: allUsers.length\n },\n binary: {\n 'report': binaryData\n }\n }\n];"
},
"typeVersion": 2
},
{
"id": "498b1bf2-1ced-4343-b459-9b6226fa8c46",
"name": "便签4",
"type": "n8n-nodes-base.stickyNote",
"position": [
2100,
80
],
"parameters": {
"color": 3,
"width": 380,
"height": 580,
"content": "#### 📄 专家第四课:创建文件"
},
"typeVersion": 1
},
{
"id": "c97edb6b-77ac-48ea-8e00-507770cc6373",
"name": "开始教程",
"type": "n8n-nodes-base.manualTrigger",
"position": [
220,
480
],
"parameters": {},
"typeVersion": 1
}
],
"pinData": {},
"connections": {
"1. Sample Data": {
"main": [
[
{
"node": "2. Split Out Users",
"type": "main",
"index": 0
}
]
]
},
"Start Tutorial": {
"main": [
[
{
"node": "1. Sample Data",
"type": "main",
"index": 0
}
]
]
},
"2. Split Out Users": {
"main": [
[
{
"node": "3. Process Each User",
"type": "main",
"index": 0
}
]
]
},
"3. Process Each User": {
"main": [
[
{
"node": "4. Fetch External Data (Advanced)",
"type": "main",
"index": 0
}
]
]
},
"5. Calculate Average Age": {
"main": [
[
{
"node": "6. Create a Binary File (Expert)",
"type": "main",
"index": 0
}
]
]
},
"4. Fetch External Data (Advanced)": {
"main": [
[
{
"node": "5. Calculate Average Age",
"type": "main",
"index": 0
}
]
]
}
}
}常见问题
如何使用这个工作流?
复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。
这个工作流适合什么场景?
中级 - 杂项
需要付费吗?
本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。
相关工作流推荐
🎓 使用并行处理优化速度关键工作流(扇出-扇入)
🎓 使用并行处理(扇出/扇入)优化速度关键工作流
If
Set
Code
+10
34 节点Lucas Peyrin
工程
带等待节点的长运行工作流状态管理系统
带等待节点的长运行工作流状态管理系统
If
Set
Code
+10
42 节点Lucas Peyrin
工程
🧑🎓 通过交互式挑战和即时反馈测试JSON技能
🧑🎓 通过交互式挑战和即时反馈测试JSON技能
If
Set
Html
+4
61 节点Lucas Peyrin
杂项
使用GPT-5 nano和Yoast SEO自动化WordPress SEO优化
使用GPT-5 nano和Yoast SEO自动化WordPress SEO优化
Set
Code
Gmail
+11
35 节点Oriol Seguí
杂项
使用Gemini RAG管道构建文档专家聊天机器人
使用Gemini RAG管道构建文档专家聊天机器人
Set
Html
Filter
+16
48 节点Lucas Peyrin
内部知识库
使用AI分析检测病毒式YouTube视频并发送邮件报告
使用AI分析检测病毒式YouTube视频并发送邮件报告
Set
Code
Sort
+14
26 节点gclbck
杂项
工作流信息
难度等级
中级
节点数量12
分类1
节点类型5
作者
Lucas Peyrin
@lucaspeyrinInnovative builder with a passion for crafting automation solutions that solve real-world challenges. From streamlining workflows to driving efficiency, my work empowers teams and individuals to achieve more with less effort. Experienced in developing scalable tools and strategies that deliver results with n8n, supabase and cline.
外部链接
在 n8n.io 查看 →
分享此工作流