Arrayクラスのmap()メソッドとfilter()メソッドの超基本

by undo
♥0 | Line 38 | Modified 2009-07-18 17:20:03 | MIT License
play

ActionScript3 source code

/**
 * Copyright undo ( http://wonderfl.net/user/undo )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/oOB0
 */

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            var tf:TextField = new TextField();
            tf.width = stage.stageWidth;
            tf.height = stage.stageHeight;
            addChild(tf);
            tf.appendText("Array.map()とArray.filter()" + "\n\n");
            
            var array:Array = ["apple",
                               "orange",
                               "banana"
                                ];
            tf.appendText("array = [" + array + "]\n");
            
            var newArray1:Array = array.map(func1);
            var newArray2:Array = array.filter(func2);
            
            tf.appendText("array.map(func1) = [" + newArray1 + "]\n");
            tf.appendText("array.filter(func2) = [" + newArray2 + "]\n");
        }
        
        private function func1(elem:Object, index:int, ary:Array):Object
        {
            //要素に"ジュース"を付加して返します。
            elem += "ジュース";
            return elem;
        }
        
        private function func2(elem:Object, index:int, ary:Array):Boolean
        {
            //文字数が6文字以上の要素を抽出します。
            if(elem.length >= 6)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}