---
title: first() – ดึงสมาชิกตัวแรกจากอาร์เรย์
url: https://www.thepexcel.com/functions/n8n/array-functions/first-n8n/
type: function-explainer
program: n8n
syntax: "$json.array.first() หรือ {{ [array].first() }}"
date: 2025-12-16
updated: 2025-12-23
scores:
  popularity: 8
  difficulty: 2
  usefulness: 8
---

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

> first() เป็น n8n array extension ที่ดึงสมาชิกตัวแรกจากอาร์เรย์ โดยไม่ต้องใช้ index notation [0] มีปร

## คำอธิบาย

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

## Syntax

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

**Variant**

```excel
$json.items.first()
```

ใช้เป็น method เมื่อ $json.items เป็น array

## เคสการใช้งาน

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

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

_เหมาะกับ:_ database-query

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

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

_เหมาะกับ:_ list-display

## ตัวอย่าง

### 1. ดึงสมาชิกตัวแรกจากลิสต์

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

**ผลลัพธ์:** `'apple'`

first() ดึงสมาชิกตัวแรก 'apple' จากอาร์เรย์ เหมือนกับ [0] แต่อ่านเข้าใจง่ายกว่า

### 2. ดึงบันทึกแรกจากข้อมูล JSON

```excel
{{ $json.records.first() }}
```

**ผลลัพธ์:** `{"id": 1, "name": "John", "email": "john@example.com"}`

ถ้า $json.records = [{id:1, name:'John'}, {id:2, name:'Jane'}] first() ดึงบันทึกแรก {id:1, name:'John'}

### 3. ดึงและเข้าถึง property ของสมาชิกแรก

```excel
{{ $json.users.first().email }}
```

**ผลลัพธ์:** `'john@example.com'`

ดึงสมาชิกแรก แล้ว access property 'email' ได้เลย ทำให้โค้ดสั้นและอ่านง่าย

### 4. ดึงจากผลค้นหา API

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

**ผลลัพธ์:** `12345`

ดึงบันทึกแรกจาก 'Query Database' node results แล้วเข้าถึง id ของมัน

## หมายเหตุเพิ่มเติม

- ใช้ first() แทน [0] เพื่อให้โค้ด readable และ expressive มากขึ้น

- ใช้ first().property หรือ first()?.property (optional chaining) เพื่อเข้าถึง properties ของสมาชิกแรก

- สหดล first() ร่วมกับ last() หรือ nth() (if available) เพื่อ iterate ตำแหน่งต่างๆ

- จำไว้ว่า first() กับ [0] เหมือนกัน แต่ first() ดูเป็น professional code มากขึ้น

## คำถามที่พบบ่อย

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

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

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

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

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

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

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

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

## แหล่งข้อมูลเพิ่มเติม

- [n8n Expressions: Array Functions](https://docs.n8n.io/code/expressions/) _(article)_
- [n8n Array Methods Documentation](https://docs.n8n.io/code/builtin/data-transformation-functions/arrays/) _(article)_
- [n8n Code Node Guide](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.code/) _(article)_

---

_Source: [https://www.thepexcel.com/functions/n8n/array-functions/first-n8n/](https://www.thepexcel.com/functions/n8n/array-functions/first-n8n/)_
