【javascript基础】之【javascript1.6 Array 新增方法】之【indexOf】
2012-04-24 18:24 sniper007 阅读(317) 评论(0) 编辑 收藏 举报
Summary
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
Method of Array |
|
---|---|
Implemented in | JavaScript 1.6 |
ECMAScript Edition | ECMAScript 5th Edition |
Syntax
array.indexOf(searchElement[, fromIndex])
Parameters
searchElement
- Element to locate in the array.
fromIndex
- The index at which to begin the search. Defaults to 0, i.e. the whole array will be searched. If the index is greater than or equal to the length of the array, -1 is returned, i.e. the array will not be searched. If negative, it is taken as the offset from the end of the array. Note that even when the index is negative, the array is still searched from front to back. If the calculated index is less than 0, the whole array will be searched.
Description
indexOf
compares searchElement
to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).
Compatibility
indexOf
is a recent addition to the ECMA-262 standard; as such it may not be present in all browsers. You can work around this by inserting the following code at the beginning of your scripts, allowing use of indexOf
in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming Object
, TypeError
, Number
, Math.floor
, Math.abs
, and Math.max
have their original value.
- if (!Array.prototype.indexOf) {
- Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
- "use strict";
- if (this == null) {
- throw new TypeError();
- }
- var t = Object(this);
- var len = t.length >>> 0;
- if (len === 0) {
- return -1;
- }
- var n = 0;
- if (arguments.length > 0) {
- n = Number(arguments[1]);
- if (n != n) { // shortcut for verifying if it's NaN
- n = 0;
- } else if (n != 0 && n != Infinity && n != -Infinity) {
- n = (n > 0 || -1) * Math.floor(Math.abs(n));
- }
- }
- if (n >= len) {
- return -1;
- }
- var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
- for (; k < len; k++) {
- if (k in t && t[k] === searchElement) {
- return k;
- }
- }
- return -1;
- }
- }
Examples
Example: Using indexOf
The following example uses indexOf
to locate values in an array.
- var array = [2, 5, 9];
- var index = array.indexOf(2);
- // index is 0
- index = array.indexOf(7);
- // index is -1
Example: Finding all the occurrences of an element
The following example uses indexOf
to find all the indices of an element in a given array, using push to add them to another array as they are found.
- var indices = [];
- var idx = array.indexOf(element);
- while (idx != -1) {
- indices.push(idx);
- idx = array.indexOf(element, idx + 1);
- }
Browser compatibility
Based on Kangax's compat tables
- Desktop
- Mobile
Feature | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 9 | (Yes) | (Yes) |
See also
我的实现
p.indexOf = function(searchElem, fromIndex){
//如果this为空指针,抛出详细类型错误
if( this === null ){
throw "【Array.prototype.indexOf ERROR】不是数组!";
}
var len = this.length;
fromIndex = Number( arguments[1] ) || 0;
//算出查询开始位置
fromIndex = fromIndex > 0 ?
Math.floor( fromIndex ) :
Math.ceil( fromIndex );
//如果开始为之比数组长度大,或者等于,返回-1
if( fromIndex >= len ){
return -1;
}
var start = fromIndex;
if( fromIndex < 0 ){
start = 0;
}
for( ; start < len; start++ ){
var $item = this[start];
if( $item === searchElem ){
return start;
}
}
return -1;
};
})(Array.prototype);