add indexOf() function to array - returns first index of a given element in an array. useful for testing, etc

git-svn-id: http://svn.openlayers.org/trunk/openlayers@225 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2006-05-21 14:42:18 +00:00
parent d87992649f
commit d694efa60e
2 changed files with 23 additions and 2 deletions

View File

@@ -519,7 +519,22 @@ Array.prototype.clear = function() {
this.length = 0;
};
/**
* @param {Object} element
*
* @returns The first index of the element in the array if found. Else returns -1
* @type int
*/
Array.prototype.indexOf = function(element) {
var index = -1;
for(var i=0; i < this.length; i++) {
if (this[i] == element) {
index = i;
break;
}
}
return index;
}
/**
* zIndex is NOT set

View File

@@ -23,7 +23,7 @@
}
function test_03_Util_Array(t) {
t.plan( 6 );
t.plan( 8 );
var array = new Array(1,2,3,4,5);
@@ -40,9 +40,15 @@
t.eq( copy.toString(), "0,1,2,4,5,6", "array.copyOf() works");
array.append(7);
t.eq( copy.toString(), "0,1,2,4,5,6", "changing a value in the copied array doesnt affect the new array");
t.eq( copy.indexOf(5), 4, "indexOf function returns index of value in an array");
t.eq( copy.indexOf(75), -1, "indexOf function returns -1 when element not found in array");
array.clear();
t.eq( array.toString(), "", "array.clear() works");
}
function test_04_Util_createDiv(t) {