indexOf()
方法返回在數組中可以找到一個給定元素的第一個索引,如果不存在,則返回-1。
let a = [2, 9, 7, 8, 9]; a.indexOf(2); // 0 a.indexOf(6); // -1 a.indexOf(7); // 2 a.indexOf(8); // 3 a.indexOf(9); // 1 if (a.indexOf(3) === -1) { // element doesn't exist in array }
語法
arr.indexOf(searchElement) arr.indexOf(searchElement[, fromIndex = 0])
參數
-
searchElement
- 要查找的元素
-
fromIndex
- 開始查找的位置。如果該索引值大於或等於數組長度,意味着不會在數組里查找,返回-1。如果參數中提供的索引值是一個負值,則將其作為數組末尾的一個抵消,即-1表示從最后一個元素開始查找,-2表示從倒數第二個元素開始查找 ,以此類推。 注意:如果參數中提供的索引值是一個負值,仍然從前向后查詢數組。如果抵消后的索引值仍小於0,則整個數組都將會被查詢。其默認值為0.
返回值
首個被找到的元素在數組中的索引位置; 若沒有找到則返回 -1
描述
indexOf
使用strict equality (無論是 ===, 還是 triple-equals操作符都基於同樣的方法)進行判斷 searchElement與
數組中包含的元素之間的關系。
應用:使用indexOf方法確定多個值在數組中的位置。
1 var array = [2, 5, 9]; 2 array.indexOf(2); // 0 3 array.indexOf(7); // -1 4 array.indexOf(9, 2); // 2 5 array.indexOf(2, -1); // -1 6 array.indexOf(2, -3); // 0
應用:找出指定元素出現的所有位置
1 var indices = []; 2 var array = ['a', 'b', 'a', 'c', 'a', 'd']; 3 var element = 'a'; 4 var idx = array.indexOf(element); 5 while (idx != -1) { 6 indices.push(idx); 7 idx = array.indexOf(element, idx + 1); 8 } 9 console.log(indices); 10 // [0, 2, 4]
應用:判斷一個元素是否在數組里,不在則更新數組
1 function updateVegetablesCollection (veggies, veggie) { 2 if (veggies.indexOf(veggie) === -1) { 3 veggies.push(veggie); 4 console.log('New veggies collection is : ' + veggies); 5 } else if (veggies.indexOf(veggie) > -1) { 6 console.log(veggie + ' already exists in the veggies collection.'); 7 } 8 } 9 10 var veggies = ['potato', 'tomato', 'chillies', 'green-pepper']; 11 12 // New veggies collection is : potato,tomato,chillies,green-papper,spinach 13 updateVegetablesCollection(veggies, 'spinach'); 14 // spinach already exists in the veggies collection. 15 updateVegetablesCollection(veggies, 'spinach');
Polyfill
indexOf
在ECMA-262 標准 的第5版中被加入,但並非所有的瀏覽器都支持該方法。你可以在編寫scripts時,在其開頭使用以下代碼,它能夠允許你在沒有本地支持的情況下使用indexOf方法。該算法符合ECMA-262第5版其中一項規定, 即假定 TypeError
和 Math.abs
呈現它們原有的價值。
1 // Production steps of ECMA-262, Edition 5, 15.4.4.14 2 // Reference: http://es5.github.io/#x15.4.4.14 3 if (!Array.prototype.indexOf) { 4 Array.prototype.indexOf = function(searchElement, fromIndex) { 5 6 var k; 7 8 // 1. Let O be the result of calling ToObject passing 9 // the this value as the argument. 10 if (this == null) { 11 throw new TypeError('"this" is null or not defined'); 12 } 13 14 var O = Object(this); 15 16 // 2. Let lenValue be the result of calling the Get 17 // internal method of O with the argument "length". 18 // 3. Let len be ToUint32(lenValue). 19 var len = O.length >>> 0; 20 21 // 4. If len is 0, return -1. 22 if (len === 0) { 23 return -1; 24 } 25 26 // 5. If argument fromIndex was passed let n be 27 // ToInteger(fromIndex); else let n be 0. 28 var n = +fromIndex || 0; 29 30 if (Math.abs(n) === Infinity) { 31 n = 0; 32 } 33 34 // 6. If n >= len, return -1. 35 if (n >= len) { 36 return -1; 37 } 38 39 // 7. If n >= 0, then Let k be n. 40 // 8. Else, n<0, Let k be len - abs(n). 41 // If k is less than 0, then let k be 0. 42 k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); 43 44 // 9. Repeat, while k < len 45 while (k < len) { 46 // a. Let Pk be ToString(k). 47 // This is implicit for LHS operands of the in operator 48 // b. Let kPresent be the result of calling the 49 // HasProperty internal method of O with argument Pk. 50 // This step can be combined with c 51 // c. If kPresent is true, then 52 // i. Let elementK be the result of calling the Get 53 // internal method of O with the argument ToString(k). 54 // ii. Let same be the result of applying the 55 // Strict Equality Comparison Algorithm to 56 // searchElement and elementK. 57 // iii. If same is true, return k. 58 if (k in O && O[k] === searchElement) { 59 return k; 60 } 61 k++; 62 } 63 return -1; 64 }; 65 }