返回某個值在數組中的第一個匹配項的索引。
## 語法
~~~
array1.indexOf(searchElement[, fromIndex])
~~~
## 參數
|參數|定義|
|--|--|
|array1|必需。一個數組對象。|
|searchElement|必需。要在 array1 中定位的值。|
|fromIndex|可選。用于開始搜索的數組索引。如果省略 fromIndex,則從索引 0 處開始搜索。|
## 返回值
數組中的 searchElement 的第一個匹配項的索引;如果未找到 searchElement,則為 -1。
## 備注
indexOf 方法在數組中搜索指定的值。該方法返回第一個匹配項的索引;如果找不到指定的值,則為 -1。
按升序索引順序執行搜索。
數組元素將與 searchElement 值進行全等比較,與 === 運算符類似。
可選 fromIndex 參數指定用于開始搜索的數組索引。如果 fromIndex 大于或等于數組長度,則返回 -1。如果 fromIndex 為負,則搜索從數組長度加上 fromIndex 的位置處開始。
下面的示例闡釋了 indexOf 方法的用法。
~~~
// Create an array. (The elements start at index 0.)
var ar = ["ab", "cd", "ef", "ab", "cd"];
// Determine the first location of "cd".
document.write(ar.indexOf("cd") + "<br/>");
// Output: 1
// Find "cd" starting at index 2.
document.write(ar.indexOf("cd", 2) + "<br/>");
// Output: 4
// Find "gh" (which is not found).
document.write (ar.indexOf("gh")+ "<br/>");
// Output: -1
// Find "ab" with a fromIndex argument of -2.
// The search starts at index 3, which is the array length plus -2.
document.write (ar.indexOf("ab", -2) + "<br/>");
// Output: 3
~~~
- ActiveXObject對象
- Array對象
- constructor屬性
- length屬性
- prototype屬性
- Array.from函數
- Array.isArray函數
- Array.of函數
- concat方法
- entries方法
- every方法
- fill方法
- filter方法
- findIndex方法
- forEach方法
- indexOf方法
- join方法
- keys方法
- lastIndexOf方法
- map方法
- pop方法
- push方法
- reduce方法
- reduceRight方法
- reverse方法
- shift方法
- slice方法
- some方法
- sort方法
- splice方法
- toString方法
- unshift方法
- valueOf方法
- values方法
- ArrayBuffer對象
- byteLength屬性
- ArrayBuffer.isView函數
- slice方法
- arguments對象
- 0...n 屬性(參數)
- callee 屬性(參數)
- length 屬性 (arguments)
- Boolean對象
- constructor 屬性(布爾值)
- prototype 屬性(布爾值)
- toString 方法 (Boolean)
- valueOf 方法 (Boolean)
- DataView對象
- buffer屬性 (DataView)
- byteLength屬性(DataView)
- byteOffset屬性(DataView)
- getInt8方法(DataView)
- getUint8方法(DataView)
- getInt16方法(DataView)
- getUint16方法(DataView)
- getInt32方法(DataView)
- getUint32方法(DataView)
- getFloat32方法(DataView)
- getFloat64方法(DataView)
- setInt8方法(DataView)
- setUint8方法(DataView)
- setInt16方法(DataView)
- setUint16方法(DataView)
- setInt32方法(DataView)
- setUint32方法(DataView)
- setFloat32方法(DataView)
- setFloat64方法(DataView)
- Date對象
- Debug對象
- Enumerator對象
- Error對象
- Float32Array對象
- Float64Array對象
- Function對象
- Global對象
- Int8Array對象
- Int16Array對象
- Int32Array對象
- Intl.Collator對象
- Intl.DateTimeFormat對象
- Intl.NumberFormat對象
- JSON對象
- Map對象
- Math對象
- Number對象
- Object對象
- Promise對象
- 代理對象
- Reflect對象
- RegExp對象
- 正則表達式對象
- Set對象
- String對象
- 符號對象
- Uint8Array對象
- Uint8ClampedArray對象
- Uint16Array對象
- Uint32Array對象
- VBArray對象
- WeakMap對象
- WeakSet對象
- WinRTError對象