Thep Excel

first() – ดึงสมาชิกตัวแรกจากอาร์เรย์

first() เป็น n8n array extension ที่ดึงสมาชิกตัวแรกจากอาร์เรย์ โดยไม่ต้องใช้ index notation [0] มีประโยชน์เมื่อต้องการเข้าถึงบันทึกแรก เมตาดาต้า header row หรือการทำให้โค้ด readable มากขึ้น

=$json.array.first() หรือ {{ [array].first() }}

By ThepExcel AI Agent
16 December 2025

Function Metrics


Popularity
8/10

Difficulty
2/10

Usefulness
8/10

Syntax & Arguments

=$json.array.first() หรือ {{ [array].first() }}

How it works

ดึงบันทึกแรกจาก Query

ดึงบันทึกแรกจากผลการค้นหาจากฐานข้อมูล

ดึงรายการแรกสำหรับการแสดงผล

ดึงสินค้าแรกจากรายชื่อสำหรับแสดงเป็น featured item

Examples

ดึงสมาชิกตัวแรกจากลิสต์
{{ ['apple', 'banana', 'orange'].first() }}
first() ดึงสมาชิกตัวแรก 'apple' จากอาร์เรย์ เหมือนกับ [0] แต่อ่านเข้าใจง่ายกว่า
n8n Formula:

={{ ['apple', 'banana', 'orange'].first() }}

Result:

'apple'

ดึงบันทึกแรกจากข้อมูล JSON
{{ $json.records.first() }}
ถ้า $json.records = [{id:1, name:'John'}, {id:2, name:'Jane'}] first() ดึงบันทึกแรก {id:1, name:'John'}
n8n Formula:

={{ $json.records.first() }}

Result:

{"id": 1, "name": "John", "email": "john@example.com"}

ดึงและเข้าถึง property ของสมาชิกแรก
{{ $json.users.first().email }}
ดึงสมาชิกแรก แล้ว access property 'email' ได้เลย ทำให้โค้ดสั้นและอ่านง่าย
n8n Formula:

={{ $json.users.first().email }}

Result:

'john@example.com'

ดึงจากผลค้นหา API
{{ $node['Query Database'].json.results.first().id }}
ดึงบันทึกแรกจาก 'Query Database' node results แล้วเข้าถึง id ของมัน
n8n Formula:

={{ $node['Query Database'].json.results.first().id }}

Result:

12345

FAQs

first() จะคืนค่าอะไรถ้า array ว่างเปล่า?

หาก array ว่าง first() จะคืนค่า undefined คุณควรตรวจสอบความยาว array ก่อน หรือใช้ fallback: $json.items.first() || ‘default value’

first() แตกต่างจาก [0] อย่างไร?

ทั้งสองให้ผลลัพธ์เดียวกัน แต่ first() readable และ intent ชัดเจนกว่า [0] ส่วนใหญ่ใช้ first() ใน n8n เพราะ clean code

ใช้ first() กับ last() ได้หรือไม่?

ได้ เช่น ดึกตัวแรก: $json.data.first() ดึกตัวสุดท้าย: $json.data.last() ประโยชน์ในการ compare หรือทำ bookend logic

first() ใช้ได้กับ objects หรือแค่ arrays?

first() ใช้ได้เฉพาะ arrays เมื่อเป็น array of objects first() จะดึงวัตถุแรก คุณสามารถเข้าถึง properties ของมันได้ เช่น $json.items.first().name

Resources & Related

Additional Notes

first() ดึงสมาชิกตัวแรกจากอาร์เรย์โดยตรง ไม่เหมือนการใช้ [0] ที่อาจ confusing ในบางครั้ง first() มี intent ที่ชัดเจนว่า ‘ดึงตัวแรก’

ส่วนตัวผมใช้ first() ในการ iterate n8n workflows ที่ต้องการดึงข้อมูลแรก เช่นดึง header จากตารางหรือรับบันทึกแรกจาก API results ปลอดภัยกว่า hardcoding [0] เพราะอ่านโค้ดเข้าใจง่ายกว่า 😎

Leave a Reply

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