Two new custom methods to the Array object.
The first one checks the whole array and deletes any duplicated values.
Array.prototype.Unique=function(){
for(var i=0;i < this.length;++i){
for(var j=(i+1);j <= this.length;++j){
if(this[i] == this[j]){
this.splice(j,1)
}
}
}
}
Usage:
myArray = new Array ("apple","pear","apple","orange");
myArray.Unique();
trace(myArray);
The second one searchs the Array for a value and returns the first position where it has been founded.
Array.prototype.SearchFirst=function(value) {
for(var i=0;i < this.length;i++){
if(this[i] == value){
return i;
}
}
return -1;
}
Usage:
myArray = new Array("apple", "pear", "banana", "orange");
applePosition = myArray.SearchFirst("apple");
if (applePosition != -1) {
trace("Apple found in position "+applePosition);
} else {
trace("Apple not found");
}
The third one is similar to the second. It now returns the last position where it has found the value.
Array.prototype.SearchLast=function(value) {
for(var i = this.length-1;i > -1;--i){
if(this[i] == value){
return i;
}
}
return -1;
}
Usage:
myArray = new Array("apple", "pear", "apple", "orange");
applePosition = myArray.SearchLast("apple");
if (applePosition != -1) {
trace("Apple found in position "+applePosition);
} else {
trace("Apple not found");
}
The first one checks the whole array and deletes any duplicated values.
Array.prototype.Unique=function(){
for(var i=0;i < this.length;++i){
for(var j=(i+1);j <= this.length;++j){
if(this[i] == this[j]){
this.splice(j,1)
}
}
}
}
Usage:
myArray = new Array ("apple","pear","apple","orange");
myArray.Unique();
trace(myArray);
The second one searchs the Array for a value and returns the first position where it has been founded.
Array.prototype.SearchFirst=function(value) {
for(var i=0;i < this.length;i++){
if(this[i] == value){
return i;
}
}
return -1;
}
Usage:
myArray = new Array("apple", "pear", "banana", "orange");
applePosition = myArray.SearchFirst("apple");
if (applePosition != -1) {
trace("Apple found in position "+applePosition);
} else {
trace("Apple not found");
}
The third one is similar to the second. It now returns the last position where it has found the value.
Array.prototype.SearchLast=function(value) {
for(var i = this.length-1;i > -1;--i){
if(this[i] == value){
return i;
}
}
return -1;
}
Usage:
myArray = new Array("apple", "pear", "apple", "orange");
applePosition = myArray.SearchLast("apple");
if (applePosition != -1) {
trace("Apple found in position "+applePosition);
} else {
trace("Apple not found");
}