ES6 - 陣列方法 forEach()



indexOf() 方法返回在陣列中找到給定元素的第一個索引,如果未找到則返回 -1。

語法

array.indexOf(searchElement[, fromIndex]);   

引數

  • searchElement − 要在陣列中查詢的元素。

  • fromIndex − 開始搜尋的索引。預設為 0,即搜尋整個陣列。如果索引大於或等於陣列的長度,則返回 -1。

返回值

返回找到元素的索引。

示例

var index = [12, 5, 8, 130, 44].indexOf(8); 
console.log("index is : " + index )  

輸出

index is : 2 
廣告
© . All rights reserved.