ActionScript Ranges?
Before I implement it by ha开发者_如何转开发nd (simple, I know), I am wondering if ActionScript or Flex support ranges?
You can do it in Ruby:
1..101
You can do it in .Net:
Enumerable.Range(1, 10)
Can you do this in AS3 or Flex? I just don't want to re-invent the wheel.
There isn't, but if you want, you can do something like this to emulate the syntax:
public class Range {
public static function get(min:int,max:int):Array {
var ret:Array = [];
while(min <= max) {
ret.push(min++);
}
return ret;
}
}
And then:
for each(var i:int in Range.get(1,10)) {
trace(i);
}
I don't believe their to be for populating an array. I checked the Array
, ArrayUtil
, and ArrayCollection
classes.
I know that theTextInput
class has a property called restrict
that you can specify a range like 0-9
or a-Z
.
Also I found this for setting a charater range: Setting Character Range
But to populate an Array I think you may just have to build a method yourself.
you can use Array's filter() method to populate a new array from a specified range of objects from the principle array.
package
{
import flash.display.Sprite;
public class Test extends Sprite
{
private var minRangeIndex:uint;
private var maxRangeIndex:uint;
public function Test()
{
var myArray:Array = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I");
minRangeIndex = 1;
maxRangeIndex = 4;
var rangeArray:Array = myArray.filter(rangeCallback);
for each (var element:Object in rangeArray)
trace(element);
}
private function rangeCallback(element:Object, index:int, array:Array):Boolean
{
return (index >= minRangeIndex && index <= maxRangeIndex);
}
}
}
//traces: B, C, D, E
using the filter() method you could actually create an array with several ranges (EX: 1-4 and 8-12) from a principal array in addition to any other type of filtering you would want, such as string matches for searching.
精彩评论