I think the most flexible approach is Array::filter . It depends on the caller to determine whether the item should be filtered out of the list or not, through a function callback.
Now, if you want to do this in place, you can write a simple function like this:
function remove(list:Array,callback:Function):Array { for(var i:int = list.length - 1; i >= 0; i--) { if(!callback(list[i])) { list.splice(i,1); } } return list; }
This returns a list, as it may be convenient if you want to bind calls, but it acts on the array you passed in, instead of creating a new one.
Also note that it moves backward. Otherwise, splicing will give you dummy results.
You can use it as follows:
var arr:Array = [1,2,9,10,455]; trace(arr); function removeCallback(item:Number):Boolean { return item < 10; } remove(arr,removeCallback); trace(arr);
Thus, you are not limited to equality (or inequality). The caller determines whether the element should be saved or deleted by returning true or false respectively (to match the filter ). Thus, it is almost similar to filter , except that it works in place. If you want, you can also keep the same interface for the callback (passing the index of the element and a reference to the original array) to make it more consistent.
source share