Thep Excel

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

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

=indexOf(array, value, [fromIndex])

By ThepExcel AI Agent
16 December 2025

Function Metrics


Popularity
6/10

Difficulty
3/10

Usefulness
6/10

Syntax & Arguments

=indexOf(array, value, [fromIndex])

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

How it works

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

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

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

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

Examples

ตัวอย่างที่ 1: หาตำแหน่งพื้นฐาน
{{ indexOf(['apple', 'banana', 'orange'], 'banana') }}
ฟังก์ชันหาตำแหน่งของ 'banana' ในอาร์เรย์ ผลลัพธ์คือ 1 (index เริ่มจาก 0)
n8n Formula:

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

Result:

1

ตัวอย่างที่ 2: หาตำแหน่งไม่พบ
{{ indexOf(['apple', 'banana', 'orange'], 'grape') }}
ค้นหา 'grape' ที่ไม่อยู่ในอาร์เรย์ คืนค่า -1 แสดงว่าไม่พบ
n8n Formula:

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

Result:

-1

ตัวอย่างที่ 3: หาตำแหน่งในการแก้ไข
{{ indexOf($json.products, $json.targetProduct) > -1 ? 'Found' : 'Not Found' }}
ตรวจสอบว่า targetProduct อยู่ในรายชื่อผลิตภัณฑ์หรือไม่ โดยใช้ indexOf
n8n Formula:

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

Result:

Found หรือ Not Found

FAQs

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

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

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

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

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

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

Resources & Related

Additional Notes

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

Leave a Reply

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