Friday, July 18, 2008

Advance Feature of Array in CS3

All of us are using Array frequently. Flash CS3 (Using AS3) has updated and incorporated so many method that has never been seen in the prior version of flash. Now the Array class has become so powerful that we can do such a big manipulation with the Array at a glance. I have tried to put some of the new feature in CS3 Array class below.

Try out below (just copy and paste)
//------------------------------------------------------------------------------------------------------------------------
1. Array.every():

/*
Executes a test function on each item in the array until an item is reached
that returns false for the specified function. You use this method to determine
whether all items in an array meet a criterion, such as having values less than
a particular number.
*/
/*
subrat comment..
When to use:-You want to determine if every element in an array meets certain criteria.
What to use:-methods accept two parameters:-
The name of a method to call which will specify your test criteria and an (optional) object which defines the scope in which the method resides.
Each method will return a Boolean true or false depending on whether or not the specified criteria is met.
The method which contains the test criteria will accept three arguments, an element of the array, the index of that particular element, and a reference to the array itself
This test method should have a Boolean for a return type.
*/
//...............Ex1
function isNumeric(element:*, index:int, arr:Array):Boolean {
return (element is Number);
}
var arr1:Array = new Array(1, 2, 4);
var res1:Boolean = arr1.every(isNumeric);
trace("isNumeric:", res1);// true

var arr2:Array = new Array(1, 2, "ham");
var res2:Boolean = arr2.every(isNumeric);
trace("isNumeric:", res2);// false
//..................Ex2
var _testArray:Array = new Array(5, 6, 2, 5, 5);
function isBelowSeven(element:int, index:int, array:Array):Boolean {
return (element <>

}
trace ('tracing 2nd ex'+_testArray.every(isBelowSeven));
//---------------------------------------------------------------------------------------

2. Array.filter()
/*
comment by subrat:
purpose : To understand Array.filter()
When:
You want to create a new array from elements in a given array
that match certain criteria.
What:
This method accepts two parameters, the name of a method to call
which will specify your test criteria and an (optional) object
which defines the scope in which the method resides. This method will
return a new array that contains each element that tests as true
according to a test method. The method which contains the test criteria
will accept three arguments, an element of the array, the index of that
particular element, and a reference to the array itself. This method
should have a Boolean for a return type (very similar to as above).
How:
Create an array of numbers that are less than 7 from an array
of various numbers with Array.filter():
*/
var _testArray:Array = new Array(5, 6, 7, 8, 9);
function isBelowSeven(element:int, index:int, array:Array):Boolean {
return (element < array =" _testArray.filter(isBelowSeven);
// traces 5,6
trace (newArray);
/*
//--------------------------------------------------------------------------

3. Array.forEach()
FOREACH:
when:
You want to call a particular method for each element in an array.

what:
Perhaps the most powerful of the new method additions,
this, like the last two methods, calls a method for each
item in an array. Unlike the last two, however, there is no
return type required - this simply calls a method for each
item in an array.

how:
create a circle for each item in an arry.

*/
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.MouseEvent;
function addCircle(element:String, index:int, array:Array):void {
var circle_mc:MovieClip = new MovieClip();
circle_mc.graphics.beginFill(0x660000);
circle_mc.graphics.drawCircle((index * 60) + 50, 60, 25);
circle_mc.graphics.endFill();
circle_mc.dynProperty = element;
circle_mc.buttonMode = true;
circle_mc.addEventListener(MouseEvent.CLICK, onClick);
addChild(circle_mc);
}
function onClick(evt:MouseEvent):void {
trace (evt.target.dynProperty);
}

var _testArray:Array = new Array("subrat", "aptara", "0673",5);

_testArray.forEach(addCircle);

//-----------------------------------------------------------------------------------------------------------------
4. Array.map()
/*
when:
You want to create a new array from elements in a given array after
performing a function on those items in the original array.

what:
This method is very similar to the Array.filter() method, however,
rather than performing a true/false test on the items in the array,
this method will alter the items of the array according to a
particular method.

how:
Create an array of upper case strings from an array of
lower case strings:

*/
//Ex1
function arrayToUpperCase(element:String, index:int, array:Array):String {
return (element.toUpperCase());
}
var _testArray:Array = new Array("one", "two", "three", "four");
var newArray:Array = _testArray.map(arrayToUpperCase);

// traces ONE,TWO,THREE,FOUR
trace (newArray);

//Ex2 :Create an array of numbers by adding 7 to each element in an original array of numbers:
function addSeven(element:Number, index:int, array:Array):Number {
return (element + 7);
}
var _testArray1:Array = new Array(1, 2, 3, 4);
var newArray1:Array = _testArray1.map(addSeven);

// traces 8,9,10,11trace (newArray1);

No comments: