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:
+16
-1
@@ -519,7 +519,22 @@ Array.prototype.clear = function() {
|
|||||||
this.length = 0;
|
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
|
* zIndex is NOT set
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_03_Util_Array(t) {
|
function test_03_Util_Array(t) {
|
||||||
t.plan( 6 );
|
t.plan( 8 );
|
||||||
|
|
||||||
var array = new Array(1,2,3,4,5);
|
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");
|
t.eq( copy.toString(), "0,1,2,4,5,6", "array.copyOf() works");
|
||||||
array.append(7);
|
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.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();
|
array.clear();
|
||||||
t.eq( array.toString(), "", "array.clear() works");
|
t.eq( array.toString(), "", "array.clear() works");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_04_Util_createDiv(t) {
|
function test_04_Util_createDiv(t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user