Sunday, October 26, 2008

Array Functions in AS3

AS3 Array class has some really handy functions that I never noticed before. They will be very useful (how did I ever NOT notice them?):

every(callback:Function, thisObject:* = null):Boolean
Executes a test function on each item in the array until an item is reached that returns false for the specified function.

filter(callback:Function, thisObject:* = null):Array
Executes a test function on each item in the array and constructs a new array for all items that return true for the specified function.

forEach(callback:Function, thisObject:* = null):void
Executes a function on each item in the array.

map(callback:Function, thisObject:* = null):Array
Executes a function on each item in an array, and constructs a new array of items corresponding to the results of the function on each item in the original array.

some(callback:Function, thisObject:* = null):Boolean
Executes a test function on each item in the array until an item is reached that returns true.

4 comments:

  1. Is there one in particular you're interested in and cannot figure out?

    ReplyDelete
  2. I would love to see an example for the forEach array function

    ReplyDelete
  3. package{
    import flash.display.Sprite;
    import flash.events.Event;
    public class Main extends Sprite{
    public function Main(){
    var dogs:Array = [
    {name: "Snoopy", breed: "Beagle" },
    {name: "Scooby", breed: "Great Dane" }
    ];
    dogs.forEach(getDogInfo);
    }
    private function getDogInfo(dog:*, index:int, arr:Array):void{
    trace(dog.name + " is a " + dog.breed);
    }
    }
    }

    Outputs:
    Snoopy is a Beagle
    Scooby is a Great Dane

    ReplyDelete