Reputation: 63
var n:Number = 0;
[1,2,3].forEach(function (x):void {
n+=x;
});
how to do it in flash? Flash throws error "function called with 3 arguments ..." i need only one argument here!
Upvotes: 1
Views: 110
Reputation: 573
The Array.forEach() waits a function as first parameter which looks like this:
function callbackFunc ( item:*, index:int, array:Array ) : void
You have to declare in your function all of the three parameters. So your stuff should look like this:
var n : Number = 0;
var arr : Array = [1,2,3];
arr.forEach(function (item:*, index:int, array:Array):void {
n+=index;
trace( "n: " + n )
});
You can't use the [1,2,3].forEach form in actionscript, because the compiler will see it as a bad metadata and throws an error.
Upvotes: 3
Reputation: 39466
Is this what you mean?
var n:Number = 0;
var ar:Array = [1,2,3];
for each(var i:Number in ar)
{
n += i;
}
trace(n);
Upvotes: 0
Reputation: 11
I am assuming your [1,2,3] is an array. In that case do it like this.
var d:Array = [1,2,3,4,5];
var v:int;
var n:Number =0;
for each(v in d )
{
n+=v;
trace(n);
}
This outputs : 1 3 6 10 15
Good luck! :)
Upvotes: 1