开发者

fill as3 array with years

开发者 https://www.devze.com 2023-04-13 03:23 出处:网络
Is there an easier way to fill an actionscript array with a range of years (let\'s say 1900 to 2000) than b开发者_运维知识库y using a for loop? I thought there\'d be some sort of range function, but I

Is there an easier way to fill an actionscript array with a range of years (let's say 1900 to 2000) than b开发者_运维知识库y using a for loop? I thought there'd be some sort of range function, but I can't find it.


Nope. A loop is the way to go.

var years:Array = [];
for (var i:int = 1900; i < 2000; i++) years.push(i);


[!] FAR WORSE SOLUTION THAN THE ACCEPTED ONE [!]

An alternative just for the heck of it:

var years : Array = new Array( 100 ).map( function( item : *, index : int, arr : Array ){ 
    return index + 1900;
} );

-- EDIT --

This was in no way a serious attempt for a better solution, since IT'S NOT! It's slower and more memory consuming. I just wondered whether it would be possible with Array#map and found it is. Since I didn't know where to dump this snippet, I posted it here.

0

精彩评论

暂无评论...
验证码 换一张
取 消