I want to return just 1 item in the filtered array
my code is
private function audioProgress(event:Event):void{
var wordindex:int=0;
function filterFun(element:int, index:int, array:Array):Boolean {
return (element < soundChannel.position);
}
var arr:Array=soundPositions.filter(filterFun);
}
I want “arr” to contains just one item
How can I do that
If I read your code correctly, you are trying to sync to a playing sound? Then using Array.filter is inefficient – you only need to keep track of the most recently passed marker.
Assuming that your soundPositions array is numerically sorted, this can be done in a simple loop:
This way, there will only be one iteration of the array – total. The while loop starts at the current index, and it will exit when the value is greater than or equal to the sound’s position, so
currentwill always point to the last item the (virtual) playhead has passed.