home |  
Sell Downloads - Ejunkie
open db network by 19.5 degrees
LYRICS | FREE E-BOOKS | SELL DOWNLOADS WITH PAYPAL
 in   
contribute for fun & profit
brink
-Array functions..
-Flash Remoting ..
-str_pad functio..
-Detecting mouse..
-Ltrim, Rtrim an..
-Opening externa..
-Modifying Flash..
-Smoothing effec..
-Array functions..
-Drawing / Vecto..
 
See all Flash Tutorials
 
-Extensions/Plug..
-FLA..
-Flash Software..
-Flash Tutorials..
 
See all Flash
 
All Resources > Flash > Flash Tutorials > ACTIONSCRIPT TUTORIALS
spread the word around  send this page to a friend   read/write comments/corrections/additions comments  rate this 

Array functions in flash - II

listed by 19.5 Degrees
 
 
Developer / Artist / Author: Ken Goulding
 
views: 12802 | rating: 4/10
 



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



« PREVIOUS
  INDEX
NEXT »

spread the word around
read comments

I want to remove all elements from an array
posted by: satya
on: Jun 24, 08 7:09 am

please help me in removing all elements from an array

post reply | read replies (0)



Alphabetic sort & multiSort
posted by: latcho
on: Oct 27, 06 8:05 pm

Found these somewhere ;)

// --------------------------
// Sort alphabetically
// case insensitive
// --------------------------
Array.prototype.asort = function()
{
var myfn;
myfn = function(element1,element2)
{
element1 = element1.toUpperCase();
element2 = element2.toUpperCase();
return element1 > element2;
}
return this.sort(myfn);
}

// ------------
// usage:
// ------------
myarray = new Array("Alessandro","Giacomo","alessio","Valerio","Paola");
myarray.asort();
trace(myarray)





// ---------------------------
// 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);

post reply | read replies (0)



Don't forget !
posted by: latcho
on: Oct 27, 06 8:00 pm

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.

post reply | read replies (0)



Wanted to say thanks
posted by: Zeke
on: Aug 3, 06 2:52 pm

I've been referring to this page periodically for a while now and just wanted to say thanks for putting it together. Good stuff.

post reply | read replies (0)



Thanks
posted by: R1
on: Sep 14, 05 5:59 am

Thanks for this elegant implementation of all these array functions...
I have also added this one to my code:

Array.prototype.callOnEach = function( func:Function )
{
var i=0;
for( i=0;i<this.length;i++ )
func.apply( this[i], null );
}

This way, you can easily perform custom actions on all elements of the array:

var a:Array = new Array( 1,2,3);
var sum = 0;
a.callOnEach( function() { sum+=this*this; } );
trace( sum );

post reply | read replies (0)



read more commentsread more comments   |   read more commentspost comment 



home | contact | contribute | terms of use | privacy policy |