Thep Excel

filter – กรองรายการจากอาร์เรย์ตามเงื่อนไข

filter() เป็น JavaScript standard array method (ES2021+, ไม่ใช่ n8n-specific) กรองรายการในอาร์เรย์ตามเงื่อนไขที่กำหนด คืนค่าอาร์เรย์ใหม่ที่มีเฉพาะรายการที่ตรงตามเงื่อนไข (predicate function returns true) มีประโยชน์ในการเลือกข้อมูลเฉพาะส่วน ลบรายการที่ไม่ต้องการ หรือจัดกลุ่มข้อมูลตามเงื่อนไข

=array.filter(predicate)

By ThepExcel AI Agent
16 December 2025

Function Metrics


Popularity
9/10

Difficulty
4/10

Usefulness
9/10

Syntax & Arguments

=array.filter(predicate)

Argument Type Required Default Description
predicate function Yes ฟังก์ชันตรวจสอบเงื่อนไข (element => condition) คืนค่า true (เก็บรายการนี้) หรือ false (ตัดออก) สำหรับแต่ละรายการในอาร์เรย์

How it works

กรองข้อมูลตามสถานะ

เลือกเฉพาะรายการที่มีสถานะเฉพาะ เช่น สินค้าที่มีในสต็อก ผู้ใช้ที่ active

กรองตามช่วงค่า

เลือกข้อมูลในช่วงที่กำหนด เช่น ราคา 100-500 บาท วันที่ภายใน 30 วัน

ลบข้อมูลที่ไม่ต้องการ

กรองออกรายการที่ null, undefined หรือไม่ผ่านเงื่อนไข

Examples

ตัวอย่างที่ 1: กรองตัวเลขมากกว่า 3
{{ [1, 2, 3, 4, 5, 6].filter(n => n > 3) }}
เลือกเฉพาะตัวเลขที่มากกว่า 3 ได้ [4, 5, 6] (ตัด 1, 2, 3 ออก)
n8n Formula:

={{ [1, 2, 3, 4, 5, 6].filter(n => n > 3) }}

Result:

[4, 5, 6]

ตัวอย่างที่ 2: กรองสินค้าที่มีในสต็อก
{{ [{ name: 'A', inStock: true }, { name: 'B', inStock: false }, { name: 'C', inStock: true }].filter(p => p.inStock) }}
เลือกเฉพาะสินค้าที่ inStock === true ได้ A และ C (ตัด B ออก)
n8n Formula:

={{ [{ name: 'A', inStock: true }, { name: 'B', inStock: false }, { name: 'C', inStock: true }].filter(p => p.inStock) }}

Result:

[{ name: 'A', inStock: true }, { name: 'C', inStock: true }]

ตัวอย่างที่ 3: กรองตามหลายเงื่อนไข
{{ $json.users.filter(u => u.active && u.age >= 18 && u.role === 'member') }}
เลือกเฉพาะผู้ใช้ที่ active, อายุ >= 18, และเป็น member
n8n Formula:

={{ $json.users.filter(u => u.active && u.age >= 18 && u.role === 'member') }}

Result:

[{ name: 'John', active: true, age: 25, role: 'member' }, ...]

ตัวอย่างที่ 4: ลบ null และ undefined
{{ [1, null, 2, undefined, 3, 0, false].filter(v => v != null) }}
กรองออก null และ undefined (ใช้ != null จะตัดทั้งคู่) เก็บ 0 และ false ไว้
n8n Formula:

={{ [1, null, 2, undefined, 3, 0, false].filter(v => v != null) }}

Result:

[1, 2, 3, 0, false]

FAQs

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

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

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

filter() คืนค่า array ของทุกรายการที่ตรงเงื่อนไข (เช่น [1, 2, 3]) ส่วน find() คืนค่ารายการแรกที่ตรงเงื่อนไข (เช่น 1) ใช้ filter() เมื่อต้องการหลายรายการ ใช้ find() เมื่อต้องการแค่รายการแรก

filter() จะคืนค่าอะไรถ้าไม่มีรายการตรงเงื่อนไข?

คืนค่า empty array [] (ไม่ใช่ null) เช่น [1, 2].filter(n => n > 10) ได้ [] สามารถเช็คด้วย arr.filter(…).length === 0

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

ใช้เมื่อต้องการเลือกรายการเฉพาะส่วนจากอาร์เรย์ เช่น กรองสินค้าที่มีในสต็อก เลือกผู้ใช้ที่ active ลบข้อมูล null/undefined หรือเลือกข้อมูลตามช่วงค่า ใช้คู่กับ map() เพื่อ transform ข้อมูลหลังกรอง

Resources & Related

Related functions

Additional Notes

filter() เป็น JavaScript standard array method (ES2021+) ที่ใช้ได้ใน n8n expressions กรองรายการในอาร์เรย์ตามเงื่อนไขที่กำหนด คืนค่าเป็นอาร์เรย์ใหม่ที่มีเฉพาะรายการที่ผ่านเงื่อนไข (predicate returns true) ไม่แก้ไขอาร์เรย์เดิม

Leave a Reply

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