Array.prototype.pushUniqueVal = function(value) {
for (var i in this) {
if (this[i] == value) {
return {success:false,pos:i};
}
}
var pos=this.length;
this[pos] = value;
return {success:true, pos:pos};
};
Array.prototype.mergeUnique=function(new_arr) {
for (var i in new_arr) {
this.pushUniqueVal(new_arr[i]);
}
}
Array.prototype.swapItems=function(one,two) {
var temp=this[one];
this[one]=this[two];
this[two]=temp;
}
Array.prototype.moveItem=function(from,to) {
this.splice(to,0,this[from]);
this.splice(from,1);
}
Array.prototype.getInd = function(value) {
for (var i in this) {
if (this[i] == value) {
return i;
}
}
};
Array.prototype.toString= function() {
if (this.length==0) {
var cnt=0;
var typ="Indexed Array"
} else {
var typ="Ordered Array"
}
var listed = "";
var addItm=function(i, itm) {
//for XML nodes, dont print the whole thing, just the top node
if (itm.hasChildNodes()) {
listed = listed + i + ":" + itm.cloneNode() + ",";
} else {
listed = listed + i + ":" + itm + ",";
}
}
if (typ=="Indexed Array") {
for (var i in this) {
cnt++;
addItm(i, this[i])
}
var tot=cnt;
} else {
for (var i = 0;i<this.length;i++) {
addItm(i, this[i])
}
var tot=this.length;
}
listed = listed.substr(0,-1);
return typ+" ("+tot+"): "+listed+")";
};
Array.prototype.remove = function(value) {
var ind=this.getInd(value);
//trace("REMOVE FROM: "+ind);
if (isNaN(ind)) {
this.removeByIndex(ind);
} else {
this.splice(ind,1);
}
};
Array.prototype.removeByIndex = function(ind) {
delete this[ind];
}
//returns total of filled slots for an array
Array.prototype.realLength = function() {
var real = 0;
for (var i in this) {
if (this[i] != undefined) {
real++;
}
}
//trace("realLength: "+real);
return real;
};
Array.prototype.contains = function(obj) {
for (var i in this) {
if (this[i]==obj) return true;
}
return false;
};
Array.prototype.copy = function() {
var new_arr = new Array();
if (this.length == 0) {
for (var i in this) {
new_arr[i] = this [i]
}
}
for (var i=0; i<this.length; i++) {
new_arr[i] = this[i]
}
return new_arr;
};
Array.prototype.clear = function() {
for (var i in this) {
this.splice(i,1);
}
};
Array.prototype.sum = function() {
var sum = 0;
for (var i in this) {
sum+=this[i];
}
return sum;
};
// ---------------------------
// Sort multiple array
// according to the first
// array
//
// If you have many arrays
// and you want to sort
// one of this mantaining
// the reference of all the
// others arrays unchanged
// use this function.
// array.multipleSort(array1[,array2,...,arrayn])
// ---------------------------
Array.prototype.multipleSort = function()
{
var mysort;
var masterArray = new Array();
for(var a = 0; a < this.length; a++)
{
masterArray[a] = new Array();
masterArray[a][0] = this[a];
for(var b = 1; b <= arguments.length; b++)
{
masterArray[a][b] = arguments[b-1][a];
}
}
mysort = function(element1,element2)
{
element1 = element1[0].toUpperCase();
element2 = element2[0].toUpperCase();
return element1 > element2;
}
masterArray.sort(mysort);
for(var a = 0; a < masterArray.length; a++)
{
this[a] = masterArray[a][0];
for(var b = 1; b < masterArray[a].length; b++)
{
arguments[b-1][a] = masterArray[a][b];
}
}
}
// -------------------------------
// usage example 1:
//
// sorting two arrays
// -------------------------------
first = new Array("Elisa","Alberto","Alessandro","Gertrud","andrea");
second = new Array("crugnola","danieli","crugnola","buckl","Gamberoni");
first.multipleSort(second);
// -------------------------------
// usage example 2
//
// sorting 3 arrays
// -------------------------------
first1 = new Array("Elisa","Alberto","Alessandro","Gertrud","andrea");
second1 = new Array("crugnola","danieli","crugnola","buckl","Gamberoni");
third1 = new Array("Gavirate","Vicenza","Padova","Munchen","Armino (Gavirate)");
first1.multipleSort(second1, third1);
All the Array protoypes mentioned here need to be hidden,
otherwise they will interfere with your results; Namely: the protoype function will become an item in the Array itself !
To hide and protect the Array prototype `copy`:
add this line under the proto:
ASSetPropFlags(Array.prototype, "copy", 7)
So the complete proto:
Array.prototype.copy = function() {
var new_arr = new Array();
if (this.length == 0) {
for (var i in this) {
new_arr[i] = this [i]
}
}
for (var i=0; i<this.length; i++) {
new_arr[i] = this[i]
}
return new_arr;
};
ASSetPropFlags(Array.prototype, "copy", 7)
Add such a line for every prototype, just change the function name in it.