---
title: $input.last() – ดึง item สุดท้ายจาก input ของ node ปัจจุบัน
url: https://www.thepexcel.com/functions/n8n/other/input-last-n8n/
type: function-explainer
program: n8n
syntax: $input.last()
date: 2025-12-16
updated: 2025-12-23
scores:
  popularity: 7
  difficulty: 2
  usefulness: 7
---

# $input.last() – ดึง item สุดท้ายจาก input ของ node ปัจจุบัน

> $input.last() ดึงเฉพาะ item สุดท้ายจากข้อมูลที่เข้ามาใน node ปัจจุบัน ใช้เมื่อต้องการสรุปผล footer ห

## คำอธิบาย

$input.last() ดึงเฉพาะ item สุดท้ายจากข้อมูลที่เข้ามาใน node ปัจจุบัน ใช้เมื่อต้องการสรุปผล footer หรือข้อมูลสุดท้ายที่เก็บไว้ทีแล้ว return เป็น item object เดียว ถ้าไม่มี items จะได้ undefined

## Syntax

```excel
$input.last()
```

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

### ดึงยอดรวมหรือ summary information ที่คำนวณแล้วส่งม...

ดึงยอดรวมหรือ summary information ที่คำนวณแล้วส่งมาเป็น item สุดท้าย

_เหมาะกับ:_ general

### Extract timestamp ล่าสุดจาก time-series data

Extract timestamp ล่าสุดจาก time-series data

_เหมาะกับ:_ general

### ดึง footer หรือ conclusion จาก dataset

ดึง footer หรือ conclusion จาก dataset

_เหมาะกับ:_ general

### เช็คสถานะสุดท้ายของ sequential process

เช็คสถานะสุดท้ายของ sequential process

_เหมาะกับ:_ general

## ตัวอย่าง

### 1. ดึงสรุปผลจาก item สุดท้าย

```excel
{{ $input.last().json.summary }}
```

**ผลลัพธ์:** `{ total: 5000, count: 25, avgValue: 200 }`

สมมติมี item หลายรายการที่คำนวณค่าต่างๆ แล้วรายการสุดท้ายเก็บสรุปผล ใช้ $input.last().json.summary ดึงข้อมูลสรุปออกมาครับ pattern นี้ใช้บ่อยในการประมวลผล report หรือ batch processing ที่ส่งผลลัพธ์เป็น item สุดท้าย

### 2. ดึง timestamp ล่าสุดจาก time-series data

```excel
{{ $input.last().json.timestamp }}
```

**ผลลัพธ์:** `2025-12-23T10:30:00Z`

เมื่อมี item หลายรายการที่เป็น time-series (เช่น sensor readings, log entries) ใช้ $input.last() ดึง item สุดท้ายได้ทันที ส่วนตัวผมใช้บ่อยเวลาต้องการรู้ว่าข้อมูลล่าสุดมาเมื่อไหร่ หรือต้อง compare ค่าเก่ากับค่าใหม่

### 3. เช็ค status สุดท้ายของการประมวลผล

```excel
{{ $input.last().json.status }}
```

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

เมื่อ workflow มีหลาย steps และ item สุดท้ายเก็บผลลัพธ์สุดท้าย ใช้ $input.last().json.status เช็คว่า process เสร็จสมบูรณ์หรือไม่ ส่วนตัวผมใช้ใน IF node เพื่อ decide ว่าต้องส่ง notification หรือ trigger action ต่อไปไหม

### 4. ใช้ร่วมกับ optional chaining ป้องกัน error

```excel
{{ $input.last()?.json?.amount ?? 0 }}
```

**ผลลัพธ์:** `5000 หรือ 0`

ใช้ ?. (optional chaining) ป้องกัน error ถ้าไม่มี items หรือไม่มี field amount และใช้ ?? ให้ค่า default ถ้าได้ undefined นี่คือ best practice ที่ควรใช้ใน production workflow เพื่อไม่ให้ workflow error ทันทีครับ

### 5. เช็คว่ามี input หรือไม่ก่อน process

```excel
{{ $input.last() ? $input.last().json.value : 'No data available' }}
```

**ผลลัพธ์:** `1500 หรือ No data available`

ใช้ ternary operator เช็คว่ามี item สุดท้ายหรือไม่ ถ้าไม่มีจะได้ undefined ซึ่งเป็น falsy value ใช้ใน IF node หรือ Switch node สำหรับ validation และ error handling ได้ดีครับ

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

- ใช้ดึง summary หรือ final result จากการประมวลผล batch ที่ s่ายมาเป็น item สุดท้าย

- ใช้ optional chaining (?.) เสมอเพื่อป้องกัน error ถ้าไม่มี items

- ใช้ร่วมกับ $input.first() เพื่อ compare ข้อมูลแรกกับสุดท้าย เช่น check price delta

- ใน Python Code node ใช้ _input.last (underscore แทน $) และเป็น property ไม่ใช่ method

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

**Q: ถ้าไม่มี items เข้ามาเลยจะเกิดอะไรขึ้น?**

จะได้ undefined กลับมาครับ ถ้าพยายามเข้าถึง .json ต่อจะเกิด error ทันที ดังนั้นควรใช้ optional chaining ($input.last()?.json) หรือเช็คด้วย ternary operator ก่อนเข้าถึงข้อมูลเสมอนะครับ

**Q: $input.last() ต่างจาก $input.all()[$input.all().length - 1] ยังไง?**

ผลลัพธ์เหมือนกันครับ แต่ $input.last() อ่านชัดเจนกว่าว่าเราต้องการ item สุดท้าย code สั้นกว่าและเข้าใจง่ายกว่า นอกจากนี้ยัง optimize performance ดีกว่าเพราะไม่ต้องเรียก all() สองครั้งแล้ว handle edge case ดีกว่า

**Q: $input.first() กับ $input.last() ต่างกันอย่างไร?**

$input.first() ดึง item แรก ส่วน $input.last() ดึง item สุดท้าย ใช้ ternary operator หรือเทคนิค arrow functions ใช้ $input.first() เมื่อต้องการ config/header ใช้ $input.last() เมื่อต้องการ summary/result สุดท้ายครับ

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

- [n8n Current Node Input Documentation](https://docs.n8n.io/code/builtin/current-node-input/) _(article)_
- [n8n Expressions Reference](https://docs.n8n.io/code/expressions/) _(article)_

---

_Source: [https://www.thepexcel.com/functions/n8n/other/input-last-n8n/](https://www.thepexcel.com/functions/n8n/other/input-last-n8n/)_
