views:

95

answers:

2

Hi All,

Is there a way to generically remove an object from an array?
(maybe not using array.filter or creating a new array)

Example:

var arr:Array= new Array();    
//create dummy objs
for (var i:uint=0; i < 10; i++){
            var someObject:SomeClassObject = new SomeClassObject();
            someObject.Name ="Amit"+ i;
            someObject.Site="http://www.mysite.com/"+i;
           //...many more props
            arr.push(someObject);
 }
//
removeElement("Amit4",arr);
removeElement("Amit8",arr);
//...so on so forth

Currently im using array.splice() to remove object

for (var i:Number=0; i < arr.length; i++)
    {
        if (arr[i].Name == element)
        {
            arr.splice(i, 1);
        }               
    }

I want to write removeElement in such a way that i can use it for different types of objects.
currently removeElement becomes dependant on implmentation..
Suppose if i want to remove a file from array of files given file name..i wud have to again write "removeElement" by changing criteria.

Also may be i can vary the criteria varing criteria? example :

arr= removeElement("Site","http://www.mysite.com/6",arr)

will remove object from arr whose "Site" property is equal to "http://www.mysite.com/6" (using above example)

ie. removeElement(criteria:object,criteria_value(s):object,arr)

Thanks All.

+1  A: 

Here is a generic function which will do what you want:

public static function removeItem(array: Array, propertyName: String, value: String): Array
{
    var newArray: Array = [];
    for (var index: int = 0; index < array.length; index++) {
        var item: Object = array[index];
        if (item && item.hasOwnProperty(propertyName)) {
            if (item[propertyName] != value)
                newArray.push(item);
        }
    }
    return newArray;
}
splash
+2  A: 

I think the most flexible approach is the one followed by Array::filter. It's up to the caller to determine whether an item should be filtered out of the list or not, through a function callback.

Now, if you want to do it in place, you could 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 the list, as it could be convenient if you wanted to chain calls, but it acts on the array you passed instead of creating a new one.

Also note that it loops backwards. Otherwise, splice will get you bogus results.

You could use it like this:

var arr:Array = [1,2,9,10,455];
trace(arr);

function removeCallback(item:Number):Boolean {
    return item < 10;
}
remove(arr,removeCallback);
trace(arr);

This way you are not restricted to equality (or inequality). The caller determines if the item should be kept or removed, by returning true or false respectively (to match filter). So, it's pretty much like filter, except it works in-place. If you want, you could also keep the same interface for the callback (passing the index of the item and a reference to the original array) to make it more coherent.

Juan Pablo Califano