Thep Excel

map – แปลงแต่ละรายการในอาร์เรย์ (transform elements)

map() เป็น JavaScript standard array method (ES2021+, ไม่ใช่ n8n-specific) แปลงแต่ละรายการในอาร์เรย์โดยใช้ callback function คืนค่าอาร์เรย์ใหม่ที่มีความยาวเท่าเดิม แต่รายการถูก transform แล้ว มีประโยชน์ในการสกัด fields, คำนวณค่าใหม่ หรือแปลงรูปแบบข้อมูล

=array.map(callback)

By ThepExcel AI Agent
16 December 2025

Function Metrics


Popularity
9/10

Difficulty
4/10

Usefulness
9/10

Syntax & Arguments

=array.map(callback)

Argument Type Required Default Description
callback function Yes ฟังก์ชันที่จะนำไปใช้กับแต่ละรายการ (element => transformedElement) คืนค่ารายการใหม่สำหรับแต่ละตำแหน่งในอาร์เรย์

How it works

สกัด fields จาก objects

ดึง field เฉพาะจากแต่ละ object เช่น ชื่อ อีเมล หรือ ID

คำนวณค่าใหม่

แปลงหรือคำนวณค่าใหม่สำหรับแต่ละรายการ เช่น ราคาลด ภาษี หรือสกุลเงิน

เปลี่ยนรูปแบบข้อมูล

แปลงข้อมูลเป็นรูปแบบใหม่ เช่น uppercase, formatted dates, หรือ restructured objects

Examples

ตัวอย่างที่ 1: สกัด field เดียว
{{ [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }].map(u => u.name) }}
ดึงเฉพาะ field 'name' จากแต่ละ object ได้ array ของชื่อ
n8n Formula:

={{ [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }].map(u => u.name) }}

Result:

['John', 'Jane']

ตัวอย่างที่ 2: คำนวณราคาลด
{{ [{ name: 'A', price: 100 }, { name: 'B', price: 200 }].map(p => ({ name: p.name, salePrice: p.price * 0.8 })) }}
สร้าง object ใหม่พร้อมชื่อและราคาลด 20% สำหรับแต่ละสินค้า
n8n Formula:

={{ [{ name: 'A', price: 100 }, { name: 'B', price: 200 }].map(p => ({ name: p.name, salePrice: p.price * 0.8 })) }}

Result:

[{ name: 'A', salePrice: 80 }, { name: 'B', salePrice: 160 }]

ตัวอย่างที่ 3: แปลงเป็น uppercase
{{ ['hello', 'world', 'test'].map(s => s.toUpperCase()) }}
แปลงทุก string เป็น uppercase โดยใช้ toUpperCase() กับแต่ละรายการ
n8n Formula:

={{ ['hello', 'world', 'test'].map(s => s.toUpperCase()) }}

Result:

['HELLO', 'WORLD', 'TEST']

ตัวอย่างที่ 4: เพิ่ม field ใหม่
{{ [1, 2, 3].map((n, index) => ({ value: n, position: index + 1, double: n * 2 })) }}
สร้าง object ใหม่พร้อม value เดิม, position, และ double value สำหรับแต่ละตัวเลข
n8n Formula:

={{ [1, 2, 3].map((n, index) => ({ value: n, position: index + 1, double: n * 2 })) }}

Result:

[{ value: 1, position: 1, double: 2 }, { value: 2, position: 2, double: 4 }, ...]

FAQs

map() สร้างอาร์เรย์ใหม่หรือแก้ไขอาร์เรย์เดิม?

สร้างอาร์เรย์ใหม่ (immutable) ไม่เปลี่ยนแปลงอาร์เรย์เดิม เช่น arr.map(x => x * 2) สร้าง array ใหม่ arr เดิมยังเหมือนเดิม

map() กับ filter() ต่างกันอย่างไร?

map() แปลงทุกรายการและคืนค่า array ความยาวเท่าเดิม (transform) ส่วน filter() เลือกเฉพาะรายการที่ตรงเงื่อนไขและคืนค่า array ที่อาจสั้นกว่า (select) ใช้ map() เมื่อต้องการ transform ใช้ filter() เมื่อต้องการ select

map() สามารถเปลี่ยน type ของรายการได้หรือไม่?

ใช่ callback สามารถคืนค่า type ใดก็ได้ เช่น [1, 2, 3].map(n => ‘Number: ‘ + n) แปลง numbers เป็น strings ได้ [‘Number: 1’, ‘Number: 2’, ‘Number: 3’]

ใช้ map() เมื่อไหร่?

ใช้เมื่อต้องการ transform แต่ละรายการในอาร์เรย์ เช่น สกัด fields จาก objects คำนวณค่าใหม่ แปลงรูปแบบข้อมูล หรือเพิ่ม fields ใหม่ ใช้คู่กับ filter() เพื่อกรองก่อน transform ใช้คู่กับ reduce() เพื่อ aggregate หลัง transform

Resources & Related

Additional Notes

map() เป็น JavaScript standard array method (ES2021+) ที่ใช้ได้ใน n8n expressions แปลงแต่ละรายการในอาร์เรย์โดยใช้ฟังก์ชันที่กำหนด คืนค่าเป็นอาร์เรย์ใหม่ที่มีความยาวเท่าเดิมแต่รายการถูกแปลงแล้ว ไม่แก้ไขอาร์เรย์เดิม

Leave a Reply

Your email address will not be published. Required fields are marked *