---
title: $itemIndex – ตำแหน่ง index ของ item ปัจจุบัน
url: https://www.thepexcel.com/functions/n8n/other/item-index-n8n/
type: function-explainer
program: n8n
syntax: $itemIndex
date: 2025-12-16
updated: 2025-12-22
scores:
  popularity: 8
  difficulty: 2
  usefulness: 8
---

# $itemIndex – ตำแหน่ง index ของ item ปัจจุบัน

> $itemIndex คือตัวแปรที่บอกตำแหน่ง (index) ของ item ปัจจุบันที่กำลังประมวลผล เริ่มนับจาก 0 (zero-base

## คำอธิบาย

$itemIndex คือตัวแปรที่บอกตำแหน่ง (index) ของ item ปัจจุบันที่กำลังประมวลผล เริ่มนับจาก 0 (zero-based) ใช้สำหรับติดตาม item ใน multi-item workflow, สร้าง running number, หรือทำ conditional logic ตามตำแหน่ง เช่น skip item แรกหรือทำอะไรพิเศษกับ item สุดท้าย

## Syntax

```excel
$itemIndex
```

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

### นับลำดับ item เพื่อสร้าง ID หรือ reference number

นับลำดับ item เพื่อสร้าง ID หรือ reference number

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

### Skip หรือ process พิเศษสำหรับ item แรก/สุดท้าย

Skip หรือ process พิเศษสำหรับ item แรก/สุดท้าย

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

### สร้าง progress indicator ในการประมวลผลข้อมูลจำนวนม...

สร้าง progress indicator ในการประมวลผลข้อมูลจำนวนมาก

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

### Batch processing โดยแบ่ง items ตามตำแหน่ง (เช่น ทุ...

Batch processing โดยแบ่ง items ตามตำแหน่ง (เช่น ทุก 10 items)

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

### Debug workflow ด้วยการ log ตำแหน่งของ item ที่เกิด...

Debug workflow ด้วยการ log ตำแหน่งของ item ที่เกิด error

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

## ตัวอย่าง

### 1. ดูตำแหน่ง item ปัจจุบัน

```excel
{{ $itemIndex }}
```

**ผลลัพธ์:** `0 (สำหรับ item แรก), 1 (item ที่สอง), ...`

แสดงตำแหน่งของ item ปัจจุบันที่กำลังถูกประมวลผล เป็น zero-based index ตามมาตรฐาน programming (item แรก = 0)

### 2. สร้างหมายเลขลำดับแบบ 1-based สำหรับแสดงผล

```excel
{{ $itemIndex + 1 }}
```

**ผลลัพธ์:** `1 (สำหรับ item แรก), 2 (item ที่สอง), ...`

เพิ่ม 1 เข้าไปเพื่อแปลงจาก zero-based (0,1,2...) เป็น one-based (1,2,3...) ที่คนทั่วไปเข้าใจง่ายกว่า ใช้เวลาต้องการแสดงลำดับให้ user เห็น เช่น "รายการที่ 1", "รายการที่ 2"

### 3. ทำ conditional logic สำหรับ item แรก

```excel
{{ $itemIndex === 0 ? 'Header' : 'Data' }}
```

**ผลลัพธ์:** `"Header" (item แรก), "Data" (item อื่นๆ)`

ใช้ ternary operator เพื่อจัดการ item แรกแตกต่างจาก items อื่น เช่น item แรกเป็น header row หรือต้องการ skip item แรก pattern นี้ใช้บ่อยมากใน data processing ที่แถวแรกเป็น column headers

### 4. สร้าง progress indicator

```excel
{{ 'Processing item ' + ($itemIndex + 1) + ' of ' + $input.all().length }}
```

**ผลลัพธ์:** `"Processing item 3 of 10"`

ผสม $itemIndex กับ $input.all().length เพื่อแสดงความคืบหน้าการประมวลผล ใช้ใน logging หรือ notification เพื่อให้รู้ว่า workflow กำลังทำงานถึงไหนแล้ว

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

- $itemIndex เป็น zero-based (0,1,2,...) เสมอ อย่าลืมบวก 1 เมื่อต้องการแสดงผลเป็นลำดับที่ 1,2,3,... สำหรับ user

- ใช้ $itemIndex % N === 0 เพื่อทำอะไรบางอย่างทุก N items เช่น $itemIndex % 10 === 0 สำหรับ batch ละ 10 รายการ

- ใช้ร่วมกับ $input.all().length เพื่อคำนวณ progress percentage: {{ Math.round($itemIndex / $input.all().length * 100) }}%

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

**Q: ทำไม item แรกเป็น 0 ไม่ใช่ 1?**

เพราะ n8n ใช้ zero-based indexing ตามมาตรฐาน JavaScript และ programming ทั่วไป ถ้าต้องการแสดงผลเป็นเลข 1,2,3... ให้บวก 1 เข้าไป เช่น {{ $itemIndex + 1 }} ครับ

**Q: ใช้ใน Code node ได้ไหม?**

ไม่ได้โดยตรงครับ $itemIndex ใช้ได้เฉพาะใน expressions เท่านั้น ใน Code node ต้องใช้วิธีอื่น เช่น loop ด้วย forEach พร้อม index parameter หรือเพิ่มฟิลด์ index ใน Set node ก่อนส่งเข้า Code node

**Q: ใช้ใน Loop Over Items node ยังไง?**

ใน Loop Over Items node ให้ใช้ {{$node["Loop Over Items"].context["currentRunIndex"]}} แทนเพื่อดู run index ของ loop ส่วน $itemIndex จะเป็น index ภายใน batch ของแต่ละ run ครับ

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

- [Expressions | n8n Docs](https://docs.n8n.io/code/expressions/) _(article)_
- [$item(index) Method Workflow Example](https://n8n.io/workflows/1330-demonstrates-the-use-of-the-dollaritemindex-method/) _(article)_

---

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