Thep Excel

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

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

=$input.last()

By ThepExcel AI Agent
16 December 2025

Function Metrics


Popularity
7/10

Difficulty
2/10

Usefulness
7/10

Syntax & Arguments

=$input.last()

How it works

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

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

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

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

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

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

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

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

Examples

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

={{ $input.last().json.summary }}

Result:

{ total: 5000, count: 25, avgValue: 200 }

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

={{ $input.last().json.timestamp }}

Result:

2025-12-23T10:30:00Z

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

={{ $input.last().json.status }}

Result:

completed

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

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

Result:

5000 หรือ 0

เช็คว่ามี input หรือไม่ก่อน process
{{ $input.last() ? $input.last().json.value : 'No data available' }}
ใช้ ternary operator เช็คว่ามี item สุดท้ายหรือไม่ ถ้าไม่มีจะได้ undefined ซึ่งเป็น falsy value ใช้ใน IF node หรือ Switch node สำหรับ validation และ error handling ได้ดีครับ
n8n Formula:

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

Result:

1500 หรือ No data available

FAQs

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

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

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

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

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

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

Resources & Related

Additional Notes

$input.last() เป็นเมธอดสำหรับดึงข้อมูลรายการสุดท้ายจากอินพุตของโหนดปัจจุบัน

ที่เจ๋งคือมันคืนค่าเป็นออบเจ็กต์รายการเดี่ยวๆ ไม่ใช่อาร์เรย์เหมือน $input.all() ทำให้ใช้งานง่ายกว่าเวลาต้องการแค่ข้อมูลชิ้นสุดท้าย ไม่ต้องคำนวณ index หรือ array length เอง

ส่วนตัวผมใช้บ่อยมากในรูปแบบ reduce operations ที่คำนวณสะสมไปเรื่อยๆ แล้วรายการสุดท้ายคือผลลัพธ์สุดท้าย หรือใน time-series data ที่ต้องการค่าล่าสุด เหมาะสำหรับดึง summary, conclusion, หรือ final result จากชุดข้อมูลครับ

ที่ต้องระวังคือถ้าไม่มีรายการเลย จะคืนค่าว่างเปล่า ดังนั้นควรเช็คด้วยเครื่องหมายคำถามจุดก่อนเข้าถึงข้อมูลนะครับ 😎

Leave a Reply

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