Getting Results with the map() Method: a map allows you to apply a function to every element of an array. : map « Array « Flash / Flex / ActionScript






Getting Results with the map() Method: a map allows you to apply a function to every element of an array.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var myArray:Array = [1,2,3,4,5];
        
        var squaredArray:Array = myArray.map(getSquare);
        trace(myArray);      // Displays: 1,2,3,4,5
        trace(squaredArray); // Displays: 1,4,9,16,25

    }
    function getSquare(elem:*, i:int, a:Array):Number {
        if (isNaN(elem)) {
            return -1;
        } else {
            return elem * elem;
        }
    }
  }
}

        








Related examples in the same category