If you ever need to test an array to see if it passes a specified criterion, then Array.every() is the method to use. This method will run a function on each array item until it returns false.
package {
import flash.display.Sprite;
public class ArrayEvery extends Sprite {
public function ArrayEvery() {
var arr1:Array = new Array(1, 2, 4);
var res1:Boolean = arr1.every(lessThan);
trace("isNumeric:", res1); // Output: true
var arr2:Array = new Array(1, 2, 50);
var res2:Boolean = arr2.every(lessThan);
trace("isNumeric:", res2); // Output: false
}
private function lessThan(element:*, index:int, arr:Array):Boolean {
return (element < 20);
}
}
}
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.