---
title: indexOf – ฟังก์ชันหาตำแหน่งของค่าในอาร์เรย์
url: https://www.thepexcel.com/functions/n8n/string-functions/indexof-n8n/
type: function-explainer
program: n8n
syntax: "indexOf(array, value, [fromIndex])"
date: 2025-12-16
updated: 2025-12-17
scores:
  popularity: 6
  difficulty: 3
  usefulness: 6
---

# indexOf – ฟังก์ชันหาตำแหน่งของค่าในอาร์เรย์

> หาตำแหน่งของค่าในอาร์เรย์

## คำอธิบาย

indexOf หาตำแหน่งแรกของค่าที่ระบุในอาร์เรย์ คืนค่า index (เริ่มตั้งแต่ 0) หากพบค่านั้น หรือ -1 หากไม่พบ ฟังก์ชันนี้มีประโยชน์ในการตรวจสอบตำแหน่ง และเข้าถึงข้อมูลใกล้เคียง

## Syntax

```excel
indexOf(array, value, [fromIndex])
```

**Variant**

```excel
$json.items.indexOf('target')
```

ใช้เป็น method เพื่อหาตำแหน่งของ 'target' ใน $json.items

**Variant**

```excel
$json.items.indexOf('target', 5)
```

เริ่มค้นหาจาก index 5 เป็นต้นไป

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| array | Yes | array |  | Array ที่ต้องการค้นหา |
| value | Yes | any |  | ค่าที่ต้องการค้นหา |
| fromIndex | No | number | 0 | ตำแหน่งเริ่มต้นสำหรับการค้นหา (default คือ 0) |

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

### หาตำแหน่งของ Item ในรายชื่อ

หาตำแหน่งของสินค้า หรือรายการใดๆ ในอาร์เรย์ เพื่อใช้ในการแก้ไขหรือลบข้อมูล

_เหมาะกับ:_ item-location

### ตรวจสอบการมีอยู่และหาตำแหน่ง

ใช้ indexOf ร่วมกับเงื่อนไข if เพื่อตรวจสอบว่ามีข้อมูลและได้ตำแหน่งพร้อมกัน

_เหมาะกับ:_ existence-check

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: หาตำแหน่งพื้นฐาน

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

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

ฟังก์ชันหาตำแหน่งของ 'banana' ในอาร์เรย์ ผลลัพธ์คือ 1 (index เริ่มจาก 0)

### 2. ตัวอย่างที่ 2: หาตำแหน่งไม่พบ

```excel
{{ indexOf(['apple', 'banana', 'orange'], 'grape') }}
```

**ผลลัพธ์:** `-1`

ค้นหา 'grape' ที่ไม่อยู่ในอาร์เรย์ คืนค่า -1 แสดงว่าไม่พบ

### 3. ตัวอย่างที่ 3: หาตำแหน่งในการแก้ไข

```excel
{{ indexOf($json.products, $json.targetProduct) > -1 ? 'Found' : 'Not Found' }}
```

**ผลลัพธ์:** `Found หรือ Not Found`

ตรวจสอบว่า targetProduct อยู่ในรายชื่อผลิตภัณฑ์หรือไม่ โดยใช้ indexOf

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

⚠️ ฟังก์ชัน indexOf() ตรวจสอบ case-sensitive และใช้ strict equality (===) ใช้ร่วมกับ includes() เพื่อตรวจสอบการมีอยู่ และ lastIndexOf() เพื่อหา occurrence สุดท้าย

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

**Q: indexOf() ค้นหาจากแต่ละ index เพื่อหา occurrence แรก หรือทั้งหมด?**

indexOf() หาเพียง occurrence แรกเท่านั้น หากต้องการหาทั้งหมด ให้ใช้ filter() หรือ map()

**Q: ถ้า indexOf() คืนค่า -1 หมายความว่าอย่างไร?**

หมายความว่าไม่พบค่าที่ระบุในอาร์เรย์ ใช้เงื่อนไข > -1 เพื่อตรวจสอบว่าพบค่าหรือไม่

**Q: ใช้ indexOf() เมื่อไหร่ในการทำงาน?**

ใช้เมื่อต้องการหาตำแหน่งของค่าในอาร์เรย์ เพื่อแก้ไข ลบ หรือดำเนินการกับข้อมูลที่พบ

## ฟังก์ชันที่เกี่ยวข้อง

- lastindexof-n8n
- [includes – ฟังก์ชันตรวจสอบว่าอาร์เรย์มีค่าใดค่าหนึ่งหรือไม่](https://www.thepexcel.com/functions/n8n/string-functions/includes-n8n/)
- find-n8n

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

- [n8n Expressions: indexOf Function](https://docs.n8n.io/code/expressions/) _(documentation)_
- [Array Searching Functions](https://docs.n8n.io/code/builtin/overview/) _(guide)_

---

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