Welcome to MoiK78 blog. Your daily intake of Internet news. This is a personal site so I post whenever I can :) but I'll try to get it daily. Be free to email me any suggestion. El bolg de Moisés García sobre tecnologías de internet y programas de diseño.
Homepage About Me Contact Me Buy at MoiK78 online shop Downloads

Welcome to MoiK78 blog, your daily intake of internet and technology news.

Picasa Web Albums My Picasa Photo Albums / Albumes de Fotos

November 13, 2002

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");
}


 
Site Meter